jQuery 如何检查下拉菜单是否被禁用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9062722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:16:59  来源:igfitidea点击:

How to check if dropdown is disabled?

jquery

提问by Beginner

Using jquery how do i simply check if it is read only?

使用 jquery 我如何简单地检查它是否是只读的?

this is what i am trying..

这就是我正在尝试的..

$("#item").keydown(function (event) {
     //alert(event.keyCode);
     if (event.keyCode == 13) {
         $("#ok").click();               
         if ($('#dropLength').prop("disabled") == false) {
             $("#dropLength").focus();
             return;
         }
         if ($('#dropUnit').prop("disabled") == false) {
             $("#dropUnit").focus();
             return;
         }
         $("#qty").focus();                
         return ;
     }
 });

The dropdowns are set to readonly using jquery also:

下拉菜单也使用 jquery 设置为只读:

if ($('#dropLength').find('option').length <= 1) {
      $('#dropLength').attr("disabled", "disabled");
}
if ($('#dropUnit').find('option').length <= 1) {
      $('#dropUnit').attr("disabled", "disabled");
}   

回答by Andreas Niedermair

The legacy solution, before 1.6, was to use .attrand handle the returned value as a bool. The main problem is that the returned type of .attrhas changed to string, and therefore the comparison with == trueis broken (see http://jsfiddle.net/2vene/1/(and switch the jquery-version)).

1.6 之前的遗留解决方案是使用.attr和处理作为bool. 主要问题是返回的类型.attr已更改为string,因此与 的比较== true被破坏(请参阅http://jsfiddle.net/2vene/1/(并切换jquery-version))。

With 1.6 .propwas introduced, which returns a bool.

.prop引入了1.6 ,它返回一个bool.

Nevertheless, I suggest to use .is(), as the returned type is intrinsically bool, like:

不过,我建议使用.is(),因为返回的类型本质上是bool,例如:

$('#dropUnit').is(':disabled');
$('#dropUnit').is(':enabled');

Furthermore .is()is much more natural (in terms of "natural language") and adds more conditions than a simple attribute-comparison (eg: .is(':last'), .is(':visible'), ... please see documentation on selectors).

此外.is(),它更自然(就“自然语言”而言),并且比简单的属性比较添加了更多条件(例如:.is(':last'), .is(':visible'), ... 请参阅有关选择器的文档)。

回答by Amar Palsapure

Try following or check demo disabledand readonly

尝试遵循或检查演示禁用只读

$('#dropUnit').is(':disabled') //Returns bool
$('#dropUnit').attr('readonly') == "readonly"  //If Condition

You can check jQuery FAQ.

您可以查看jQuery 常见问题解答

回答by Manish Shrivastava

There are two options:

有两种选择:

First

第一的

You can also use like is()

你也可以使用像 is()

$('#dropDownId').is(':disabled');

Second

第二

Using == trueby checking if the attributes value is disabled. attr()

使用== true通过检查的属性值disabledattr()

$('#dropDownId').attr('disabled');

whatever you feel fits better , you can use :)

无论你觉得更适合什么,你都可以使用:)

Cheers!

干杯!