Removing the "disabled" Attribute with jQuery
In the given code, an attempt is made to disable inputs and then enable them again upon clicking a link. However, the solution doesn't function as expected.
The Solution:
To effectively remove the "disabled" attribute, use the prop() method instead of removeAttr(). The correct code would be:
$("#edit").click(function(event){ event.preventDefault(); $('.inputDisabled').prop("disabled", false); // Element(s) are now enabled. });
Why Use prop()?
prop() should be used for properties like "disabled," while attr() should be used for attributes. In pre-jQuery 3.0 versions, removeAttr() would also set the corresponding boolean property to false, leading to incorrect behavior in modern browsers. prop(), on the other hand, only sets the property value without affecting the attribute.
Additional Note:
jsFiddle Example: https://jsfiddle.net/
The above is the detailed content of Why Use Prop() Instead of RemoveAttr() to Disable Form Inputs?. For more information, please follow other related articles on the PHP Chinese website!