Javascript 使用jQuery禁用空选择框
如何使用jQuery禁用所有没有任何选项的select输入?我正在使用.each访问每个框,但我很难确定如何将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 () {
此
与选择器组合以指定该此
中的选项
$('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');
}
})