jQuery set然后在textbox上读取readonly attr

fea*_*net 5 jquery attributes jquery-selectors

?<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
Run Code Online (Sandbox Code Playgroud)

除了这里的问题,我无法使解决方案工作,因为似乎jQuery没有正确设置readonly属性(或至少一致).

注意我得到相同的结果 $('#test').attr('readonly', true);

我正在使用Chrome,而readonly属性呈现为readonly="".这里还有另一篇文章,建议FireFox做同样的事情.

我没有太多关心这一点,因为它仍然阻止文本框可编辑,但我无法找到检测readonly属性的方法.

我通常喜欢jQuery,但这有点像WTF.

有任何想法吗?

Tra*_*ian 7

我个人不会使用removeAttr()...我只是用attr()将它设置为false.

然后,您可以正常查询属性:

if ($("#X").attr("readonly"))
{
    execute code
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以继续使用removeAttr(),然后只使用长度作为检测属性的方法:

if (!$("#X").attr("readonly").length)
{
    execute code
}
Run Code Online (Sandbox Code Playgroud)

如果你担心选择器,你应该选择语法作为:not([readonly='readonly']) 编辑:我看到你已经在你引用的答案中.