使用jquery如何简单地检查它是否只读?
这就是我想要的......
$("#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将下拉列表设置为只读:
if ($('#dropLength').find('option').length <= 1) {
$('#dropLength').attr("disabled", "disabled");
}
if ($('#dropUnit').find('option').length <= 1) {
$('#dropUnit').attr("disabled", "disabled");
}
答案 0 :(得分:101)
1.6之前的传统解决方案是使用.attr
并将返回的值作为bool
处理。主要问题是返回的.attr
类型已更改为string
,因此与== true
的比较被破坏(请参阅http://jsfiddle.net/2vene/1/(并切换jquery-version) )。
引入了1.6 .prop
,返回bool
。
尽管如此,我建议使用.is()
,因为返回的类型本质上是bool
,如:
$('#dropUnit').is(':disabled');
$('#dropUnit').is(':enabled');
此外.is()
更自然(就“自然语言”而言)并且比简单的属性比较增加了更多条件(例如:.is(':last')
,.is(':visible')
,...请参阅documentation on selectors)。
答案 1 :(得分:7)
$('#dropUnit').is(':disabled') //Returns bool
$('#dropUnit').attr('readonly') == "readonly" //If Condition
您可以查看jQuery FAQ。
答案 2 :(得分:1)
有两种选择:
<强>第一强>
您也可以使用is()
$('#dropDownId').is(':disabled');
<强>第二强>
通过检查属性值是否为== true
来使用disabled
。 attr()
$('#dropDownId').attr('disabled');
无论你感觉更合适,你都可以使用:)
干杯!
答案 3 :(得分:0)
我正在搜索类似的内容,因为我必须检查我所有选择中的哪些被禁用了。
所以我用这个:
let select= $("select");
for (let i = 0; i < select.length; i++) {
const element = select[i];
if(element.disabled == true ){
console.log(element)
}
}