【发布时间】:2011-04-12 05:23:26
【问题描述】:
<input id="test" type="text" value="text" />
alert($(':input:not([readonly])').length); // 1
$('#test').attr('readonly', 'readonly');
alert($(':input:not([readonly])').length); // 1
$('#test').removeAttr('readonly');
alert($(':input:not([readonly])').length); // 1
除了the question here,我无法让解决方案正常工作,因为 jQuery 似乎没有正确(或至少一致地)设置 readonly 属性。
请注意,我与$('#test').attr('readonly', true); 得到相同的结果
我正在使用 Chrome,只读属性呈现为 readonly=""。这里有另一篇文章表明 FireFox 也是如此。
我对此并不在意,因为它仍然阻止文本框可编辑,但我找不到检测只读属性的方法。
我通常喜欢 jQuery,但这有点 WTF。
有什么想法吗?
【问题讨论】:
-
您看到这种行为的原因是因为
[readonly]选择器只检查属性的存在,而true或false存在.
标签: jquery attributes jquery-selectors