重置 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
Reset Particular Input Element in a HTML Form
提问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
, password
and textarea
, plus defaultChecked
on checkbox
/radio
and defaultSelected
on option
. Curiously, notavailable on hidden
(except in IE, where it's considered a bug).
可用的text
,password
并且textarea
,再加defaultChecked
上checkbox
/radio
与defaultSelected
上option
。奇怪的是,不可用hidden
(除了在 IE 中,它被认为是一个错误)。