Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从jQuery中的select元素按值获取选项_Jquery_Jquery Selectors_Option - Fatal编程技术网

从jQuery中的select元素按值获取选项

从jQuery中的select元素按值获取选项,jquery,jquery-selectors,option,Jquery,Jquery Selectors,Option,我想访问选择选项jQuery对象,如下所示: var $options = $('select').children(); var $someOption = $('[value="some_value"]', $options); var $anotherOption = $('[value="another_value"]', $options); 但是$someOption或$anotherOption看起来都不像$('select')中的任何元素 我相信我知道如何组合单行选择器,但由于

我想访问选择选项jQuery对象,如下所示:

var $options = $('select').children();
var $someOption = $('[value="some_value"]', $options);
var $anotherOption = $('[value="another_value"]', $options);
但是
$someOption
$anotherOption
看起来都不像
$('select')
中的任何元素

我相信我知道如何组合单行选择器,但由于我正在访问各种选项,我想使用
$options
句柄来提高可读性和性能

代码和/或原理有什么问题?

您必须使用jQuery的方法:


要使用上下文参数,请不要调用
.children()


您是否意外地将
$
$someOption
$anotherOption
的选择器中排除?您的第二个和第三个选择器上的jQuery函数似乎缺少
$
。@ExplosionPills,jmoerdyk,谢谢,我现在修复了操作错误。将jQuery对象作为第二个参数传递,作为要选择的上下文,因此它与您编辑的代码的操作相同(此语句未更正)@JustinBicknell-Nope。传入上下文搜索子体,并:
$options.find('[value=“some_value”]')
这将每次遍历DOM。过滤只查看被过滤的集合。@JosephSilber为True,虽然它只需要遍历select下面的DOM子集,而不是整个DOM,所以还不算太糟。为True,但它比过滤有什么好处?@JosephSilber它不太冗长,有些人可能会发现它更可读,这是OP寻找的标准之一。
var $options = $('select').children();
var $someOption = $options.filter('[value="some_value"]');
var $anotherOption = $options.filter('[value="another_value"]');
var $select = $('select');
var $someOption = $('[value="some_value"]', $select);
var $anotherOption = $('[value="another_value"]', $select);