开发者

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;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜