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

jua*_*ito 4 jquery jquery-selectors option

我想访问select选项jQuery对象:

var $options = $('select').children();
var $someOption = $('[value="some_value"]', $options);
var $anotherOption = $('[value="another_value"]', $options);
Run Code Online (Sandbox Code Playgroud)

但也$someOption还是$anotherOption像任何元素$('select').

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

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

Jos*_*ber 5

你必须使用jQuery的filter方法:

var $options = $('select').children();
var $someOption = $options.filter('[value="some_value"]');
var $anotherOption = $options.filter('[value="another_value"]');
Run Code Online (Sandbox Code Playgroud)

  • @JustinBicknell - 不.在上下文中传递搜索后代,[等同于此](http://jsfiddle.net/P8QAj/):`$ options.find('[value ="some_value"]')` (3认同)