$input.disabled = true;
或
$input.disabled = "disabled";
标准方法是什么?反之,如何启用禁用的输入?
最佳答案
jQuery 1.6+
要更改 disabled
属性,您应该使用 .prop()
功能。
$("input").prop('disabled', true);
$("input").prop('disabled', false);
jQuery 1.5 及以下
.prop()
函数不存在,但是.attr()
类似:
设置禁用属性。
$("input").attr('disabled','disabled');
要重新启用,正确的方法是使用.removeAttr()
$("input").removeAttr('disabled');
在任何版本的 jQuery 中
如果您只处理一个元素,您始终可以依赖实际的 DOM 对象,并且可能比其他两个选项快一点:
// assuming an event handler thus 'this'
this.disabled = true;
使用 .prop()
或 .attr()
方法的优点是您可以为一堆选定的项目设置属性。
注意:在 1.6 中有一个 .removeProp()
听起来很像 removeAttr()
的方法,但它不应该在 'disabled'
等原生属性上使用 摘自文档:
Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.
事实上,我怀疑这种方法有很多合法用途, bool Prop 的完成方式是您应该将它们设置为 false,而不是像 1.5 中的“属性”对应物那样“删除”它们
关于javascript - 使用 jQuery 禁用/启用输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1414365/