简体   繁体   中英

How do you clear a plain HTML form after onsubmit?

After the onsubmit event has fired and the form has been submitted (via an action="mailto:yournamehere@somebox.com") , is there a way to then clear the form, close the window or what? Is there a post submit that I can hook into via JavaScript. This must be a client side only solution.

A quick and easy solution:

<form action="mailto:email@email.com"
    onsubmit="this.submit(); this.reset(); return false;">
function submit_form() {
document.formu1.submit();
document.formu1.reset(); 
}
<form name="formu1" action="action-page" method="POST" target="_blank" />
Name: <input type="text" name="name" /><br /><br />
Pass: <input type="password" name="pass" /><br /><br />
<input type="button" value="Send" onclick="submit_form();" />
</form>

There is no such event.

You could hack it with something along the lines of:

// Begin anonymous wrapper function to limit the scope of variables
// and avoid namespace collisions.
(function () {
  // Get a quick reference to the form. 
  var f = document.forms.myForm;
  // Define a submit handler that won't actually
  // do anything until after the form has "submitted"
  f.onsubmit = function () {
    setTimeout(f.reset, 1000);
  }
}());

… but action="mailto:…" is poorly supported, so you should use a real form handler instead of trying to hack around the limitations of mailto: .

You can simply put:

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

in your onSubmit() handler function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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