javascript 使用javascript重置值

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

reset value using javascript

javascript

提问by Lucky13

Hi guys i want to reset(to old) the values of multiple text boxes when i click on check box. please help

嗨,伙计们,当我单击复选框时,我想重置(旧)多个文本框的值。请帮忙

回答by Spliffster

In Javascript you can restore a default value of an input field like this:

在 Javascript 中,您可以像这样恢复输入字段的默认值:

var myinput = document.getElementById("myinput");
myinput.value = myinput.defaultValue;

HTH

HTH

回答by Shadow Wizard is Ear For You

I guess you mean restore the original values of the textboxes? If so, here is the required code:

我猜你的意思是恢复文本框的原始值?如果是这样,这里是所需的代码:

<script type="text/javascript">
function RestoreValues() {
   var arrInputs = document.getElementsByTagName("input");
   for (var i = 0; i < arrInputs.length; i++) {
       var oCurInput = arrInputs[i];
       if (oCurInput.type == "text")
           oCurInput.value = oCurInput.defaultValue;
   }
}
</script>

To restore the values just call the function, for example:

要恢复值只需调用该函数,例如:

<button type="button" onclick="RestoreValues();">Restore</button>

Live test case: http://jsfiddle.net/yahavbr/Nakjv/2/

现场测试用例:http: //jsfiddle.net/yahavbr/Nakjv/2/

Edit: using defaultValuesave lots of code. :)

编辑:使用defaultValue保存大量代码。:)