Clear input fields on form submit

≯℡__Kan透↙ 提交于 2020-07-28 06:34:05

问题


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.

I have of course a input tag, type submit, and its working fine. But I also already have a specific onSubmit function :

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!


回答1:


Use the reset function, which is available on the form element.

var form = document.getElementById("myForm");
form.reset();



回答2:


You can clear out their values by just setting value to an empty string:

var1.value = '';
var2.value = '';



回答3:


By this way, you hold a form by his ID and throw all his content. This technic is fastiest.

document.forms["id_form"].reset();



回答4:


Still using empty strings you can use:

document.getElementById("name").value = '';
document.getElementById("review").value = '';


来源:https://stackoverflow.com/questions/15343890/clear-input-fields-on-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!