如果select有选项,如何检查jquery?

Geo*_*rge 20 html jquery

如果select有选项,如果不禁用它并显示[not items],如何检查jquery?

Nat*_*son 35

一种方法:

if ($('#myselect option').length == 0) {
    $('#myselect').hide();
}
Run Code Online (Sandbox Code Playgroud)


jAn*_*ndy 6

var $select = $('input:select option');
if(!$select.length )
    $select.attr('disabled', true);
Run Code Online (Sandbox Code Playgroud)

要么:

$('select:not(:has(option))').attr('disabled', true);
Run Code Online (Sandbox Code Playgroud)

  • 第一个将测试页面上有多少选择元素...而不是它们有多少选项.我不认为`input:select`存在.应该只是`选择`. (2认同)

Sco*_*tyG 5

另一种方法是简单地使用 jQuery has函数

if ($('#myselect').has('option').length == 0) 
{
    // Hide and disable select input here
}
Run Code Online (Sandbox Code Playgroud)

有关 jQuery has函数的信息,请参见:https : //api.jquery.com/has/