使用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 ;
}
});
Run Code Online (Sandbox Code Playgroud)
使用jquery将下拉列表设置为readonly:
if ($('#dropLength').find('option').length <= 1) {
$('#dropLength').attr("disabled", "disabled");
}
if ($('#dropUnit').find('option').length <= 1) {
$('#dropUnit').attr("disabled", "disabled");
}
Run Code Online (Sandbox Code Playgroud)
And*_*air 107
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');
Run Code Online (Sandbox Code Playgroud)
而且.is()
是更自然的(在"自然语言"的条款),并不仅仅是一个简单的属性相比增加了更多的条件(例如:.is(':last')
,.is(':visible')
,...请参见上选择文件).
$('#dropUnit').is(':disabled') //Returns bool
$('#dropUnit').attr('readonly') == "readonly" //If Condition
Run Code Online (Sandbox Code Playgroud)
你可以查看jQuery FAQ.