Javascript 如何启用禁用的文本字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8484181/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:10:47  来源:igfitidea点击:

How to enable a disabled text field?

javascripthtmlformsvalidationonsubmit

提问by Hassan Z

I wanna know how do I enable a disabled form text field on submit. Also I wanna make sure if user goes back to form or click reset field will show again as disabled.

我想知道如何在提交时启用禁用的表单文本字段。此外,我想确保用户返回表单或单击重置字段将再次显示为禁用状态。

I tried to use

我试着用

document.pizza.field07.disabled = false ;

It does disables the field, by clicking reset or hitting back button still keeps it enable.

它确实禁用了该字段,通过单击重置或回击按钮仍然保持启用状态。

Please guide.

请指导。

回答by Adam Rackis

To access this element in a more standard way, use document.getElementByIdwith setAttribute

要以更标准的方式访问此元素,请使用document.getElementByIdsetAttribute

document.getElementById("field07").setAttribute("disabled", false);

EDIT

编辑

Based on your comment, it looks like field07 is a name, not an id. As such, this should be what you want:

根据您的评论,看起来 field07 是name,而不是 id 。因此,这应该是您想要的:

var allfield7s = document.getElementsByName("field07");
for (var i = 0; i < allfield7s.length; i++)
    allfield7s[i].setAttribute("disabled", false);

回答by zajc3w

That is the only working solution for Me:

这是我唯一可行的解​​决方案:

var allfield7s = document.getElementsByName("field07");
for (var i = 0; i < allfield7s.length; i++)
    allfield7s[i].removeAttribute("disabled");

回答by Bins Jose

You can enable a disabled html control with the following JavaScript code.

您可以使用以下 JavaScript 代码启用禁用的 html 控件。

document.getElementById('elementId').removeAttribute('disabled');

document.getElementById('elementId').removeAttribute('disabled');

回答by Purag

You can also do this with jQuery:

你也可以用jQuery做到这一点:

$(function(){
    $("[name='field07']").prop("disabled", false);
});

We simply select all the elements where the nameattribute is field07(using name because you said so in the comments of @AdamRackis's answer) and set its disabledproperty to false.

我们只需选择name属性所在的所有元素field07(使用名称,因为您在@AdamRackis 的回答的评论中是这么说的)并将其disabled属性设置为false.

More about prop().

更多关于prop().