如何禁用使用jQuery没有任何选项的所有select输入?我使用一个.each来访问每个框,但是我很难找到如何将this与选择器结合起来来指定该this中的选项。
$('select').each(function(){
if (this 'option').length == 0) {
$(this).attr('disabled','disabled');
}
})发布于 2014-08-24 02:09:37
$('select').each(function(){
if (!this.options.length) this.disabled = true;
});您也可以这样做:
$('select').prop('disabled', function () {
return !this.options.length;
});发布于 2014-08-24 02:14:12
demo
您可以使用这个衬垫:
$('select:not(:has(option))').prop('disabled', true);发布于 2014-08-24 02:10:20
上下文(this)是第二个参数。
$('select').each(function(){
if ($('option', this).length == 0) {
this.disabled = true;
}
})https://stackoverflow.com/questions/25467937
复制相似问题