重置 HTML 表单中的特定输入元素

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

Reset Particular Input Element in a HTML Form

htmlformsreset

提问by SKR

I have multiple input textboxes in my page. I want to reset particular text box to its onload state if certain conditions fails. I am considering using a Hidden element to store the onload state of the textbox. I would other suggestions or solutions to resolve this issue.

我的页面中有多个输入文本框。如果某些条件失败,我想将特定文本框重置为其加载状态。我正在考虑使用 Hidden 元素来存储文本框的加载状态。我会提出其他建议或解决方案来解决此问题。

回答by bobince

defaultValue:

defaultValue

<input type="text" value="initial" id="field">
<button id="reset">reset</button>
<script type="text/javascript">
    document.getElementById('reset').onclick= function() {
        var field= document.getElementById('field');
        field.value= field.defaultValue;
    };
</script>

Available on text, passwordand textarea, plus defaultCheckedon checkbox/radioand defaultSelectedon option. Curiously, notavailable on hidden(except in IE, where it's considered a bug).

可用的textpassword并且textarea,再加defaultCheckedcheckbox/radiodefaultSelectedoption。奇怪的是,不可hidden(除了在 IE 中,它被认为是一个错误)。