Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery prevent multiple submit

Tags:

jquery

I want to prevent multiple submit if someone click on one of the submit buttons multiple times.

How can unbind or undelgate in this case the call of my custom function do_some_stuff that is only happens one time, because I try some of the jquery methods, but I think I did something wrong. Thanks

$(function() {
    $('div.ajax').delegate('form button', 'click', function(e) {
        $(this).do_some_stuff();
        e.preventDefault();
    });
});
like image 759
Azd325 Avatar asked Mar 15 '12 12:03

Azd325


People also ask

How do I stop multiple form submission in JQuery?

The code: jQuery. validator. setDefaults({ submitHandler: function(form){ // Prevent double submit if($(form).

How to prevent multiple clicks on submit button in JQuery?

on() method is the preferred method for attaching event handlers to a document. To answer your question about multiple submits, another new addition in JQuery 1.7 is the . one() handler which, attaches an event handler to an object but only allows it to be fired once. This will allow you to prevent multiple submits.

How to prevent double submit in PHP?

You can as well prevent this by having a unique field like email/username or registration number. Then test then new field against the existing one in the DB, and if it matches, then the user has registered before, and form submission should stop, otherwise continue submitting form.


1 Answers

Bind and unbind are deprecated in JQuery.

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

http://api.jquery.com/on/

To answer your question about multiple submits, another new addition in JQuery 1.7 is the .one() handler which, attaches an event handler to an object but only allows it to be fired once. This will allow you to prevent multiple submits.

e.g:

$("form#form1").one("submit", submitFormFunction);

function submitFormFunction(event) {
    event.preventDefault(); 
    $("form#form1").submit();
}

Note I'm binding to the form submit event rather than a button click event.

like image 68
reach4thelasers Avatar answered Oct 25 '22 10:10

reach4thelasers