javascript 清除表单提交上的输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15343890/
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
Clear input fields on form submit
提问by Kizer Yakuza
I know this is continuously asked anew, and I've checked out different answers and tried different solutions but to no avail. In some cases it can be really be a case by case thing depending on how the code has been redacted.
我知道这会不断被重新询问,我查看了不同的答案并尝试了不同的解决方案,但无济于事。在某些情况下,这可能真的是个案,具体取决于代码的编辑方式。
I'd like to simply have the two input fields in my page to clear when I click submit (&& enter - unless its a separate event, that's for another thread), considering I always press enter as opposed to clicking - this is a site just meant for my use.
当我单击提交时,我只想清除页面中的两个输入字段(&& enter - 除非它是一个单独的事件,这是另一个线程),考虑到我总是按 Enter 而不是单击 - 这是一个站点仅供我使用。
I have of course a input tag, type submit, and its working fine. But I also already have a specific onSubmit function :
我当然有一个输入标签,输入提交,它工作正常。但我也已经有了一个特定的 onSubmit 函数:
function onSubmitForm(e) {
e.preventDefault();
var var1 = document.getElementById("name");
var var2 = document.getElementById("review");
arrayOne.push( {name:var1.value,review:var2.value} );
localStorage.filmDatabase = JSON.stringify(arrayOne);
document.getElementById("eyedee").innerHTML += '<p>' + '<span class=name>' + var1.value + '</span>' + '<span class=review>' + var2.value + '</span>';
}
I'm guessing its just about putting one line of (right) code, most probably at the end of the block but I just couldn't figure it out. Thanks a lot for the help!
我猜它只是放置一行(正确的)代码,很可能在块的末尾,但我就是想不通。非常感谢您的帮助!
回答by thomaux
回答by Rob M.
You can clear out their values by just setting value to an empty string:
您可以通过将 value 设置为空字符串来清除它们的值:
var1.value = '';
var2.value = '';
回答by Damby Green
By this way, you hold a form by his ID and throw all his content. This technic is fastiest.
这样,你拿着一张他的ID的表格,扔掉他的所有内容。这种技术是最快的。
document.forms["id_form"].reset();
回答by megamind
Still using empty strings you can use:
仍然使用空字符串,您可以使用:
document.getElementById("name").value = '';
document.getElementById("review").value = '';