javascript 防止双击表单提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23752433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:29:55  来源:igfitidea点击:

Prevent double click on form submission

javascriptjquery

提问by TK91

I want to prevent double click on submit button.

我想防止双击提交按钮。

My code is...

我的代码是...

onclick='this.style.visibility = "hidden";'

But it also hidden if i enter wrong entry. I want to disable button on double click but button enable in validation wrong entry

但如果我输入错误,它也会隐藏。我想在双击时禁用按钮,但在验证错误输入时启用按钮

回答by Runcorn

use this instead :

改用这个:

onclick="this.disabled=true;"

And for Double click event you could use :

对于双击事件,您可以使用:

ondblclick="this.disabled=true;"

回答by Kaja Mydeen

  jQuery.fn.preventDoubleSubmission = function() {

var last_clicked, time_since_clicked;

$(this).bind('submit', function(event){

if(last_clicked) 
  time_since_clicked = event.timeStamp - last_clicked;

last_clicked = event.timeStamp;

if(time_since_clicked < 2000)
  return false;

return true;
});
};

Using like this:

像这样使用:

 $('#my-form').preventDoubleSubmission();

Prevent double submission of forms in jQuery

防止在 jQuery 中重复提交表单

回答by Joby Joseph

Your question is not clear. But based on my understanding, the tips below may help you.

你的问题不清楚。但根据我的理解,以下提示可能会对您有所帮助。

To disable button on double click you can write code for that in .dblclick() jquery event. if there is any validation error, enable the button again.

要在双击时禁用按钮,您可以在 .dblclick() jquery 事件中为此编写代码。如果有任何验证错误,请再次启用该按钮。