ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 如何使用jQuery删除“禁用”属性?

2019-09-15 13:46:56  阅读:151  来源: 互联网

标签:javascript jquery disabled-input


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

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

HTML

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

jQuery的:

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

这显示我是真的然后是假的,但输入没有任何变化:

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

解决方法:

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

在你的情况下,它将是:

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

jsFiddle example here.

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

基本上,在获取或设置属性时应使用prop()(例如自动播放,检查,禁用和其他必需).

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

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

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

“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.”

“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.”

标签:javascript,jquery,disabled-input
来源: https://codeday.me/bug/20190915/1804940.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有