jQuery - get select with exactly 2 options
I need 开发者_运维百科to find all select's with exactly 2 options. The code I'm using is:
$('select.ct option:nth-child(2)')
but it seems to get everything with more than 2 options.
This will also work:
$('select:has(option:first-child + option:last-child)');
Basically it looks for a select
element that contains an option
element that is the first child and is adjacent to an option
that is the last child.
var selects = $('select').filter(function() {
return $(this).find('option').length === 2
});
or:
var selects = $('select').filter(function() {
return $('option', this).length === 2;
});
精彩评论