JQuery form submit, prevent submitting multiple times after the first submit

I am making a web application that uses jquery submit to request some data, but I do not want people to submit the form several times after submitting it for the first time. So I want that after submitting, the next submit does nothing for a few seconds.

$("#updatestock_form").submit(function(e){ // requesting my data.. }); 

How can i achieve this?

+6
source share
4 answers

"So, I want that after submitting, the next submit does nothing for a few seconds."

If you mean that you want the user to ignore the second and subsequent attempts to send, until after a certain number of seconds you can do this in several ways. Here is the first thing that came to mind:

 var allowSubmit = true; $("#updatestock_form").submit(function(e){ if (!allowSubmit) return false; allowSubmit = false; setTimeout(function(){ allowSubmit = true; }, 5000); // your existing submit code here // requesting my data.. }); 

That is, there is a flag indicating whether it is currently allowed. On a send event, if this flag is false, simply return false immediately to cancel the send event. Otherwise (if currently true ) set the flag to false , set the timeout to change the flag back, say, 5 seconds, and then continue with the existing send code.

+8
source
 $("#updatestock_form").submit(function(e){ $(this).unbind("submit") $("#submitButton").attr("disabled", "disabled") }); 

Very simple.

+2
source

Are you making an ajax request to submit? So create a lock variable var locked; and set it locked = true; when sending and reset to false when the ajax call is complete. (sucess condition)

0
source

Disable button onclick event button

 $("#buttonId").bind('click', function() { $(this).attr("disabled", "disabled"); }); 

and remove the disabled attribute after a successful operation

0
source

All Articles