如何删除“已禁用"使用jQuery的属性? [英] How to remove "disabled" attribute using jQuery?

查看:109
本文介绍了如何删除“已禁用"使用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:

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

jQuery:

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


这显示了true然后是false,但输入内容没有变化:


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

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

推荐答案

始终使用 prop() 方法,用于在使用jQuery时启用或禁用元素(原因见下文).

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

在您的情况下,它将是:

In your case, it would be:

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

jsFiddle示例.

为什么可以使用attr()/removeAttr()来使用prop()?

基本上,在获取或设置属性(例如autoplaycheckeddisabledrequired等)时应使用prop().

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

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

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

虽然可以使用attr()/removeAttr()完成 的操作,但这并不意味着它应该完成 (并且可能会引起奇怪的/有问题的行为,例如在这种情况下.)

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).

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

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

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

"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."

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

"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."

这篇关于如何删除“已禁用"使用jQuery的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆