简体   繁体   English

提交后清除我的表单输入

[英]Clearing my form inputs after submission

I've tried it a few different ways based on searches I've done on the subject and for some reason I can't get it to work.我已经根据我对该主题所做的搜索尝试了几种不同的方法,但由于某种原因我无法让它工作。 I just want my text inputs and textarea to clear after I hit the submit button.我只想在我点击提交按钮后清除我的文本输入和 textarea。

Here's the code.这是代码。

<div id="sidebar-info">
    <form name="contact-form" action="formprocess.php" method="post" target="ninja-frame">
        <h1>By Phone</h1>
        <p id="by-phone">XXX-XXX-XXXX</p>
        <h1>By Email</h1>
        <p id="form-row">Name</p>
        <input name="name" id="name" type="text" class="user-input" value="">
        <p id="form-row">Email</p>
        <input name="email" id="email" type="text" class="user-input" value="">
        <p id="form-row">Message</p>
        <textarea name="message" id="message" class="user-input" rows="10" maxlength="1500"></textarea>
        <p>*Please fill out every field</p>
        <input type="submit" value="Submit" id="submit" onclick="submitForm()">

        <script>
            function submitForm() {
            document.contact-form.submit();
            document.contact-form.reset();
            }
        </script>
    </form>
</div>

Your form is being submitted already as your button is type submit .您的表单已被提交,因为您的按钮类型为submit Which in most browsers would result in a form submission and loading of the server response rather than executing javascript on the page.这在大多数浏览器中会导致表单提交和服务器响应加载,而不是在页面上执行 javascript。

Change the type of the submit button to a button .将提交按钮的类型更改为button Also, as this button is given the id submit , it will cause a conflict with Javascript's submit function.此外,由于此按钮被赋予了 id submit ,它会导致与 Javascript 的提交功能发生冲突。 Change the id of this button.更改此按钮的 id。 Try something like尝试类似的东西

<input type="button" value="Submit" id="btnsubmit" onclick="submitForm()">

Another issue in this instance is that the name of the form contains a - dash.这种情况下的另一个问题是表单的名称包含一个-破折号。 However, Javascript translates - as a minus.然而,Javascript 翻译-作为一个减号。

You will need to either use array based notation or use document.getElementById() / document.getElementsByName() .您将需要使用基于数组的符号或使用document.getElementById() / document.getElementsByName() The getElementById() function returns the element instance directly as Id is unique (but it requires an Id to be set). getElementById()函数直接返回元素实例,因为 Id 是唯一的(但它需要设置一个 Id)。 The getElementsByName() returns an array of values that have the same name. getElementsByName()返回具有相同名称的值的数组。 In this instance as we have not set an id, we can use the getElementsByName with index 0.在本例中,由于我们没有设置 id,我们可以使用索引为 0 的getElementsByName

Try the following尝试以下

function submitForm() {
   // Get the first form with the name
   // Usually the form name is not repeated
   // but duplicate names are possible in HTML
   // Therefore to work around the issue, enforce the correct index
   var frm = document.getElementsByName('contact-form')[0];
   frm.submit(); // Submit the form
   frm.reset();  // Reset all form data
   return false; // Prevent page refresh
}

since you are using jquery library, i would advise you utilize the reset() method.由于您使用的是jquery库,我建议您使用reset()方法。

Firstly, add an id attribute to the form tag首先,给form标签添加一个id属性

<form id='myForm'>

Then on completion, clear your input fields as:然后完成后,将输入字段清除为:

$('#myForm')[0].reset();

You can try this:你可以试试这个:

function submitForm() {
  $('form[name="contact-form"]').submit();
  $('input[type="text"], textarea').val('');
}

This script needs jquery to be added on the page.此脚本需要在页面上添加 jquery。

The easiest way would be to set the value of the form element.最简单的方法是设置表单元素的值。 If you're using jQuery (which I would highly recommend) you can do this easily with如果您使用 jQuery(我强烈推荐),您可以轻松地使用

$('#element-id').val('')

For all input elements in the form this may work (i've never tried it)对于表单中的所有输入元素,这可能有效(我从未尝试过)

$('#form-id').children('input').val('')

Note that .children will only find input elements one level down.请注意, .children 只会找到下一级的输入元素。 If you need to find grandchildren or such .find() should work.如果你需要找到孙子或这样的 .find() 应该工作。

There may be a better way however this should work for you.可能有更好的方法,但这应该适合您。

Try this:试试这个:

$('#contact-form input[type="text"]').val('');
$('#contact-form textarea').val('');

I used the following with jQuery $("#submitForm").val("");我在 jQuery $("#submitForm").val("");使用了以下内容$("#submitForm").val("");

where submitForm is the id for the input element in the html.其中submitForm是 html 中输入元素的 id。 I ran it AFTER my function to extract the value from the input field.我在我的函数之后运行它以从输入字段中提取值。 That extractValue function below:下面那个extractValue函数:

function extractValue() { var value = $("#submitForm").val().trim(); console.log(value); };

Also don't forget to include preventDefault();也不要忘记包含preventDefault(); method to stop the submit type form from refreshing your page!阻止提交类型表单刷新页面的方法!

只需在函数末尾包含这一行,问题就很容易解决了!

document.getElementById("btnsubmit").value = "";
var btnClear = document.querySelector('button');
var inputs = document.querySelectorAll('input');
 
btnClear.addEventListener('click', () => {
    inputs.forEach(input =>  input.value = '');
});

您可以根据MDN使用HTMLFormElement.prototype.reset

document.getElementById("myForm").reset();

You can assign to the onsubmit property:您可以分配给onsubmit属性:

document.querySelector('form').onsubmit = e => {
   e.target.submit();
   e.target.reset();
   return false;
};

https://developer.mozilla.org/docs/Web/API/GlobalEventHandlers/onsubmit https://developer.mozilla.org/docs/Web/API/GlobalEventHandlers/onsubmit

 btnSave.addEventListener("click", () => { inputs.forEach((input) => (input.value = "")); });

**Use the button you want to clear the inputs after clicking on it instead of the btnSave ** **点击后使用要清除输入的按钮而不是 btnSave **

Try this:试试这个:

function submitForm () {
    // your code

    $('form :input').attr('value', '');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM