使用JQuery删除disabled属性? [英] Remove disabled attribute using JQuery?

查看:111
本文介绍了使用JQuery删除disabled属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须首先禁用输入,然后点击链接启用它们。

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示例。


为什么要使用 prop()何时可以使用 attr() / removeAttr()来执行此操作?

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

基本上,获取或设置时应使用 prop() em> properties (例如 autoplay 已选中已禁用必需等)。

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

通过使用 removeAttr(),您将完全删除已禁用属性本身 - 而 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属性。示例包括输入元素的
属性,输入的已禁用属性和
按钮,或复选框的已选中属性。 .prop()方法
应用于设置已禁用已选中而不是 .attr()
方法。 .val()方法应该用于获取和设置

"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删除disabled属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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