javascript从html输入中删除“禁用”属性

时间:2020-12-26 19:36:35

How can I remove the "disabled" attribute from an HTML input using javascript?

如何使用javascript从HTML输入中删除“禁用”属性?

<input id="edit" disabled>

at onClick I want my input tag to not consist of "disabled" attribute.

在onClick我希望我的输入标签不包含“禁用”属性。

4 个解决方案

#1


129  

Set the element's disabled property to false:

将元素的disabled属性设置为false:

document.getElementById('my-input-id').disabled = false;

If you're using jQuery, the equivalent would be:

如果你正在使用jQuery,那么等效的是:

$('#my-input-id').prop('disabled', false);

For several input fields, you may access them by class instead:

对于多个输入字段,您可以通过类访问它们:

var inputs = document.getElementsByClassName('my-input-class');
for(var i = 0; i < inputs.length; i++) {
    inputs[i].disabled = false;
}

Where document could be replaced with a form, for instance, to find only the elements inside that form. You could also use getElementsByTagName('input') to get all input elements. In your for iteration, you'd then have to check that inputs[i].type == 'text'.

例如,可以用表单替换文档,以便仅查找该表单中的元素。您还可以使用getElementsByTagName('input')来获取所有输入元素。在你的迭代中,你必须检查输入[i] .type =='text'。

#2


19  

Why not just remove that attribute?

为什么不删除该属性?

  1. vanilla JS: elem.removeAttribute('disabled')
  2. vanilla JS:elem.removeAttribute('disabled')
  3. jQuery: elem.removeAttr('disabled')
  4. jQuery:elem.removeAttr('禁用')

#3


2  

To set the disabled to false using the name property of the input:

要使用输入的name属性将disabled设置为false:

document.myForm.myInputName.disabled = false;

#4


0  

method 1 <input type="text" onclick="this.disabled=false;" disabled>
<hr>
method 2 <input type="text" onclick="this.removeAttribute('disabled');" disabled>
<hr>
method 3 <input type="text" onclick="this.removeAttribute('readonly');" readonly>

code of the previous answers don't seem to work in inline mode, but there is a workaround: method 3.

以前的答案的代码似乎不能在内联模式下工作,但有一种解决方法:方法3。

see demo https://jsfiddle.net/eliz82/xqzccdfg/

看演示https://jsfiddle.net/eliz82/xqzccdfg/

#1


129  

Set the element's disabled property to false:

将元素的disabled属性设置为false:

document.getElementById('my-input-id').disabled = false;

If you're using jQuery, the equivalent would be:

如果你正在使用jQuery,那么等效的是:

$('#my-input-id').prop('disabled', false);

For several input fields, you may access them by class instead:

对于多个输入字段,您可以通过类访问它们:

var inputs = document.getElementsByClassName('my-input-class');
for(var i = 0; i < inputs.length; i++) {
    inputs[i].disabled = false;
}

Where document could be replaced with a form, for instance, to find only the elements inside that form. You could also use getElementsByTagName('input') to get all input elements. In your for iteration, you'd then have to check that inputs[i].type == 'text'.

例如,可以用表单替换文档,以便仅查找该表单中的元素。您还可以使用getElementsByTagName('input')来获取所有输入元素。在你的迭代中,你必须检查输入[i] .type =='text'。

#2


19  

Why not just remove that attribute?

为什么不删除该属性?

  1. vanilla JS: elem.removeAttribute('disabled')
  2. vanilla JS:elem.removeAttribute('disabled')
  3. jQuery: elem.removeAttr('disabled')
  4. jQuery:elem.removeAttr('禁用')

#3


2  

To set the disabled to false using the name property of the input:

要使用输入的name属性将disabled设置为false:

document.myForm.myInputName.disabled = false;

#4


0  

method 1 <input type="text" onclick="this.disabled=false;" disabled>
<hr>
method 2 <input type="text" onclick="this.removeAttribute('disabled');" disabled>
<hr>
method 3 <input type="text" onclick="this.removeAttribute('readonly');" readonly>

code of the previous answers don't seem to work in inline mode, but there is a workaround: method 3.

以前的答案的代码似乎不能在内联模式下工作,但有一种解决方法:方法3。

see demo https://jsfiddle.net/eliz82/xqzccdfg/

看演示https://jsfiddle.net/eliz82/xqzccdfg/