清除表单提交中的输入字段

时间:2021-10-26 20:30:00

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键而不是点击 - 这是一个站点只是供我使用。

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!

我猜它只是放一行(右)代码,最有可能在块的末尾,但我无法弄明白。非常感谢您的帮助!

4 个解决方案

#1


34  

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

使用表单元素上提供的reset函数。

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

#2


34  

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

您只需将值设置为空字符串即可清除其值:

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

#3


2  

Still using empty strings you can use:

仍然使用空字符串,您可以使用:

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

#4


0  

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

通过这种方式,你可以通过他的身份证持有一张表格,然后抛出所有内容。这项技术最快。

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

#1


34  

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

使用表单元素上提供的reset函数。

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

#2


34  

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

您只需将值设置为空字符串即可清除其值:

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

#3


2  

Still using empty strings you can use:

仍然使用空字符串,您可以使用:

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

#4


0  

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

通过这种方式,你可以通过他的身份证持有一张表格,然后抛出所有内容。这项技术最快。

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