问题描述
$input.disabled = true;
或
$input.disabled = "disabled";
哪种标准方式?而且,相反,如何启用禁用输入?
Which is the standard way? And, conversely, how do you enable a disabled input?
推荐答案
jQuery 1.6 +
要更改已禁用
属性,您应使用功能。
jQuery 1.6+
To change the disabled
property you should use the .prop()
function.
$("input").prop('disabled', true);
$("input").prop('disabled', false);
jQuery 1.5及以下
.prop()
功能不存在,但类似:
jQuery 1.5 and below
The .prop()
function doesn't exist, but .attr()
does similar:
设置禁用属性。
$("input").attr('disabled','disabled');
要再次启用,正确的方法是使用
To enable again, the proper method is to use .removeAttr()
$("input").removeAttr('disabled');
在任何版本的jQuery中
你如果你只处理一个元素,它总是可以依赖于实际的DOM对象,并且可能比其他两个选项快一点:
In any version of jQuery
You can always rely on the actual DOM object and is probably a little faster than the other two options if you are only dealing with one element:
// assuming an event handler thus 'this'
this.disabled = true;
使用 .prop()
或 .attr()
方法是您可以为一堆选定项目设置属性。
The advantage to using the .prop()
or .attr()
methods is that you can set the property for a bunch of selected items.
注意:在1.6中有听起来很像 removeAttr()
的方法,但它不应该被使用 on native property on 'disabled'
摘自文档:
Note: In 1.6 there is a .removeProp()
method that sounds a lot like removeAttr()
, but it SHOULD NOT BE USED on native properties like 'disabled'
Excerpt from the documentation:
事实上,我怀疑这种方法有很多合法用途, boolean props以这样的方式完成,你应该将它们设置为false,而不是像1.5中的属性对应物那样删除它们
In fact, I doubt there are many legitimate uses for this method, boolean props are done in such a way that you should set them to false instead of "removing" them like their "attribute" counterparts in 1.5
这篇关于使用jQuery禁用/启用输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!