开发者

jQuery - get select with exactly 2 options

开发者 https://www.devze.com 2022-12-08 08:48 出处:网络
I need 开发者_运维百科to find all select\'s with exactly 2 options. The code I\'m using is: $(\'select.ct option:nth-child(2)\')

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

精彩评论

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