Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery禁用空选择框_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery禁用空选择框

Javascript 使用jQuery禁用空选择框,javascript,jquery,Javascript,Jquery,如何使用jQuery禁用所有没有任何选项的select输入?我正在使用.each访问每个框,但我很难确定如何将此与选择器组合以指定该此中的选项 $('select').each(function(){ if (this 'option').length == 0) { $(this).attr('disabled','disabled'); } }) 您还可以执行以下操作: $('select').prop('disabled', function () {

如何使用jQuery禁用所有没有任何选项的select输入?我正在使用.each访问每个框,但我很难确定如何将
与选择器组合以指定该
中的选项

$('select').each(function(){
    if (this 'option').length == 0) {
        $(this).attr('disabled','disabled');
    }
})
您还可以执行以下操作:

$('select').prop('disabled', function () {
    return !this.options.length;
});
上下文(
)是第二个参数

$('select').each(function(){
    if ($('option', this).length == 0) {
        this.disabled = true;
    }
})

您可以使用这一行:

$('select:not(:has(option))').prop('disabled', true);
代码如下:

$('select').each(function(){
    var noOfOptions = $(this).children('option').size();

    if(noOfOptions == 0){
        $(this).attr('disabled', 'true');
    }
})

没有理由在DOM中重新查询选项。OP询问如何将
与选择器组合。
$('select').each(function(){
    var noOfOptions = $(this).children('option').size();

    if(noOfOptions == 0){
        $(this).attr('disabled', 'true');
    }
})