如何检查下拉菜单是否被禁用? [英] How to check if dropdown is disabled?
问题描述
使用jQuery,我如何简单地检查它是否为只读?
Using jquery how do i simply check if it is read only?
这就是我正在尝试的..
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 ;
}
});
还使用jquery将下拉列表设置为只读:
The dropdowns are set to readonly using jquery also:
if ($('#dropLength').find('option').length <= 1) {
$('#dropLength').attr("disabled", "disabled");
}
if ($('#dropUnit').find('option').length <= 1) {
$('#dropUnit').attr("disabled", "disabled");
}
推荐答案
1.6之前的旧解决方案是使用.attr
并将返回的值作为bool
处理.主要问题是返回的.attr
类型已更改为string
,因此与== true
的比较中断了(请参见 http://jsfiddle.net/2vene/1/(并切换jquery版本).
The legacy solution, before 1.6, was to use .attr
and handle the returned value as a bool
. The main problem is that the returned type of .attr
has changed to string
, and therefore the comparison with == true
is broken (see http://jsfiddle.net/2vene/1/ (and switch the jquery-version)).
1.6版引入了.prop
,它返回了bool
.
With 1.6 .prop
was introduced, which returns a bool
.
尽管如此,我建议使用 .is()
,因为返回的类型本质上是bool
,例如:
Nevertheless, I suggest to use .is()
, as the returned type is intrinsically bool
, like:
$('#dropUnit').is(':disabled');
$('#dropUnit').is(':enabled');
此外,.is()
更自然(就自然语言"而言),并且比简单的属性比较(例如:.is(':last')
,.is(':visible')
,...)添加了更多条件,请参阅关于选择器的文档).
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).
这篇关于如何检查下拉菜单是否被禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!