Javascript javascript从html输入中删除“禁用”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11719961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript remove "disabled" attribute from html input
提问by Jam Ville
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 我希望我的输入标签不包含“禁用”属性。
回答by dxh
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'
.
document
例如,可以用表单替换Where以仅查找该表单内的元素。您还可以使用getElementsByTagName('input')
来获取所有输入元素。在您的for
迭代中,您必须检查inputs[i].type == 'text'
.
回答by Dragos Rusu
Why not just remove that attribute?
为什么不删除该属性?
- vanilla JS:
elem.removeAttribute('disabled')
- jQuery:
elem.removeAttr('disabled')
- 香草JS:
elem.removeAttribute('disabled')
- jQuery:
elem.removeAttr('disabled')
回答by Henry Hedden
To set the disabled
to false using the name
property of the input:
disabled
使用name
输入的属性将 设置为 false :
document.myForm.myInputName.disabled = false;
回答by crisc82
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。