Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript - 如何使用jQuery删除“disabled”属性?(How to remove “disabled” attribute using jQuery?)

I have to disable inputs at first and then on click of a link to enable them.

(我必须首先禁用输入,然后单击链接以启用它们。)

This is what I have tried so far, but it doesn't work.

(这是我到目前为止所尝试的,但它不起作用。)

HTML:

(HTML:)

<input type="text" disabled="disabled" class="inputDisabled" value="">

jQuery:

(jQuery的:)

$("#edit").click(function(event){
   event.preventDefault();
   $('.inputDisabled').removeAttr("disabled")
});


This shows me true and then false but nothing changes for the inputs:

(这显示我是true然后是false但输入没有任何变化:)

$("#edit").click(function(event){
   alert('');
   event.preventDefault();
   alert($('.inputDisabled').attr('disabled'));
   $('.inputDisabled').removeAttr("disabled");
   alert($('.inputDisabled').attr('disabled'));
});
  ask by fatiDev translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Always use the prop() method to enable or disable elements when using jQuery (see below for why).

(在使用jQuery时, 始终使用prop()方法启用或禁用元素(请参阅下面的原因)。)

In your case, it would be:

(在你的情况下,它将是:)

$("#edit").click(function(event){
   event.preventDefault();
   $('.inputDisabled').prop("disabled", false); // Element(s) are now enabled.
});

jsFiddle example here.

(这里有jsFiddle示例。)


Why use prop() when you could use attr() / removeAttr() to do this?

(为什么在使用attr() / removeAttr()执行此操作时使用prop() ?)

Basically, prop() should be used when getting or setting properties (such as autoplay , checked , disabled and required amongst others).

(基本上,在获取或设置属性时应使用prop() (例如autoplaycheckeddisabled和其他required )。)

By using removeAttr() , you are completely removing the disabled attribute itself - while prop() is merely setting the property's underlying boolean value to false.

(通过使用removeAttr() ,您将完全删除disabled属性本身 - 而prop()仅将属性的基础布尔值设置为false。)

While what you want to do can be done using attr() / removeAttr() , it doesn't mean it should be done (and can cause strange/problematic behaviour, as in this case).

(虽然您想要做的事情可以使用attr() / removeAttr() ,但这并不意味着它应该完成(并且可能导致奇怪/有问题的行为,如本例所示)。)

The following extracts (taken from the jQuery documentation for prop() ) explain these points in greater detail:

(以下摘录(摘自prop()jQuery文档 )更详细地解释了这些要点:)

"The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes."

(“在特定情况下,属性和属性之间的区别很重要。在jQuery 1.6之前, .attr()方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。从jQuery 1.6开始, .prop()方法提供了一种显式检索属性值的方法,而.attr()检索属性。“)

"Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value ."

(“属性通常会影响DOM元素的动态状态,而不会更改序列化的HTML属性。例如输入元素的value属性,输入和按钮的disabled属性,或复选框的checked属性.prop()方法应该用于设置disabledchecked而不是.attr()方法。应该使用.val()方法来获取和设置value 。“)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...