$("#select option:second").val() jquery - 保持选择框选项的第二个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6713143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$("#select option:second").val() jquery - keep select second value of select box option
提问by zod
I am able to keep select the first and last value of a select box
我可以保持选择选择框的第一个和最后一个值
$("#selbox").val($("#sel_selbox option:last").val());
$("#selbox").val($("#selbox option:last").val());
How can i keep select the second value ?
我怎样才能保持选择第二个值?
$("#selbox").val($("#selbox option:second").val());
THis is giving error. Is there any option to keep select by giving index ?
这是给出错误。是否有任何选项可以通过提供 index 来保持选择?
回答by Michael Edenfield
Yes, you want the special jQuery eq()
selector.
是的,您需要特殊的 jQueryeq()
选择器。
$("#selbox").val($("#selbox option:eq(1)").val());
回答by lonesomeday
The selector way of doing this is :eq()
, as already suggested.
However, this is not the best method. Because eq
is a non-standard, jQuery-specific extension, the whole query has to be parsed by jQuery's selector engine. None of the selection is handled by the browser's native selection capabilities. This means that the selection will be muchslower.
然而,这并不是最好的方法。因为它eq
是一个非标准的、特定于 jQuery 的扩展,所以整个查询必须由 jQuery 的选择器引擎解析。浏览器的本机选择功能不处理任何选择。这意味着选择会多慢。
If you make a simple selection and then cut it down with jQuery methods, you will get much better performance, because the browser's native selection function (called querySelectorAll
) will handle everything that it can, which will provide a big speed boost.
如果您进行一个简单的选择,然后使用 jQuery 方法将其删除,您将获得更好的性能,因为浏览器的本机选择函数(称为querySelectorAll
)将处理它所能处理的一切,这将大大提高速度。
$('#selbox option').eq(1).val();
$('#selbox option').last().val();
$('#selbox option').first().val(); // same as .eq(0).val();
See:
看:
回答by Will
or like this:
或者像这样:
$('#selbox').find('option').eq(1).val();
回答by Flapper
I needed to hide all but the first 2 options and used this.
我需要隐藏除前两个选项之外的所有选项并使用它。
$("#DDLId").find('option:not(:first)').not(":first").map(function ()
{
return $(this).parent('span').length == 0 ? this : null;
}).wrap('<span>').hide();
NB this uses the "hide with a span" trick rather than simply using hide() as IE won't hide options directly.
注意,这使用了“用跨度隐藏”技巧,而不是简单地使用 hide(),因为 IE 不会直接隐藏选项。
use .unwrap().show()
to reshow the hidden options.
用于.unwrap().show()
重新显示隐藏选项。
回答by Eric
Another way of writing it.
另一种写法。
$("#selbox").val($("#selbox option").eq(1).val());