简体   繁体   English

成功提交表单后禁用表单

[英]Disable Form after Successful form Submit

We're having problems with an open source third party application. 我们在使用开源第三方应用程序时遇到问题。 Many of the forms take awhile to submit and people are pressing the 'submit' button multiple times. 许多表格需要一段时间才能提交,并且人们多次按下“提交”按钮。 Many of these forms are already using onsubmit to validate the forms. 其中许多表格已经使用onsubmit来验证表格。 We don't want to harm any of that. 我们不想伤害任何一个。

At first, we thought we could just disable submit buttons once clicked. 起初,我们以为只要单击一下就可以禁用提交按钮。 The problem is if the form doesn't validate, then the button is still disabled. 问题是,如果表单无法通过验证,则该按钮仍处于禁用状态。

We also can't just use a basic onsubmit because we would overwrite the current validation and other processes. 我们也不能只使用基本的onsubmit,因为我们会覆盖当前的验证和其他过程。

Is there a way that we can disable the submit button only after validatation when the form actually begins successful submission? 有没有一种方法可以让我们在表单真正开始成功提交后才在验证后禁用“提交”按钮?

如果您使用的是jQuery,则调用您的validate函数,如果失败,则只需重新启用该按钮即可。

Since you have no control over the third-party app, one approach may be to disable each button on click, and then re-enable that button after a certain period – long enough for the validate routine to complete. 由于您无法控制第三方应用程序,因此一种方法可能是禁用单击时的每个按钮,然后在一定时间后重新启用该按钮-足够长的时间才能完成验证例程。 If the validate function is successful, the form will submit, at which point there's no button to re-enable. 如果验证功能成功,则将提交表单,这时没有按钮可以重新启用。

This should get you going: 这应该可以帮助您:

$('button').on('click',function() {
  $(this).prop('disabled', true);
  (function(b) {
     setTimeout(function() {
       b.prop('disabled', false);
     },1000);
   }
  )($(this));
});

Fiddle: http://jsfiddle.net/x3tv6vqo/ 小提琴: http : //jsfiddle.net/x3tv6vqo/

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

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