簡體   English   中英

onsubmit 后如何清除純 HTML 表單?

[英]How do you clear a plain HTML form after onsubmit?

在 onsubmit 事件觸發並提交表單后(通過 action="mailto:yournamehere@somebox.com"),有沒有辦法清除表單,關閉窗口或什么? 是否有我可以通過 JavaScript 掛鈎的帖子提交。 這必須是客戶端唯一的解決方案。

一個快速簡便的解決方案:

<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>

沒有這樣的事件。

你可以用以下方式破解它:

// 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);
  }
}());

...但是 action="mailto:..." 支持很差,所以你應該使用真正的表單處理程序,而不是試圖繞過mailto:限制

你可以簡單地說:

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

在您的onSubmit()處理函數中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM