Self-proclaimed greatness is a hard thing to prove.
Prevent double submit with jQuery

Another great “little” solution with jQuery. Needed a simple way to protect all the forms on our site from being double submitted. There was an attempted solution in place on our app, that added an onsubmit to every form with a function above it called ignoreDoubleSubmit that tried to trap the event of the button was clicked and not allow it to be clicked again. The problem was the event for the button wasn’t present when the form was submitted so it would silently error out with a blink in the Firebug console. After weeks of fighting to have it fixed, I removed the code and reimplemented a working solution with jQuery
$('form').submit(function(){
$(':submit', this).click(function() {
return false;
});
});
| Print article | This entry was posted by webguy on September 16, 2008 at 7:58 pm, and is filed under DOM. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 7 years ago
this will not only work for submit buttons but for also image buttons. comments welcomed.
$(“form”).each(function(){
var $that = $(this);
$that.submit(function(){
$that.find(“input[type='image'],input[type='submit']“).attr(“disabled”, “true”);
});
});
about 7 years ago
Tony,
Thanks improvements are always welcome. I have run into a slight problem with the approach of disabling buttons during the submit, in some cases developers are using the name of the button clicked to control the routing of the next action, when you disable the buttons, the name is not passed. This is easily remedied by attaching to the click event:
$(“form”).each(function(){
var $that = $(this);
$that.submit(function(){
$that.find(“input[type='image'],input[type='submit']“).click(function(){
return false;
});
});
});
about 1 year ago
If you take the combination of the two approaches an even more elegant solution is possible
$(’form’).submit(function(){
$(this).find(”input[type='image'],input[type='submit']“).click(function(){
return false;
});
});
about 1 year ago
I prefer this: http://henrik.nyh.se/2008/07/jquery-double-submission
about 1 year ago
Very good and simple solution!
Additionally, the developer can use an alert (or something) to inform the user that the request is being processed.
about 7 months ago
Hello,
This solution:
$(’form’).submit(function(){
$(this).find(”input[type='image'],input[type='submit']“).click(function(){
return false;
});
});
does a good job, but how do you enable the submit button again after a return from an ajax call? I thought do this in the sucess function would fix it but it doesn’t
$(this).find(“input[type='image'],input[type='submit']“).click(function(){
return true;
});
about 7 months ago
Randy, your problem is probably that $(this) is a reference to the object calling the function. In the original, that is the form. But in your ajax success function, it most likely refers to something else. So it’s trying to “find” image and submit inputs inside whatever $(this) refers to and not finding them. If you replace $(this) in your ajax success callback with a selector that points to your form (probably just using its ID), it might fix the problem.
about 7 months ago
Henrik said he preferred another solution without saying why. I checked it out, and I see its advantages. While webguy’s solution elegantly prevents a submit _button_ from being double clicked, it does not handle all ways a form can be submitted, most importantly by pressing the enter key. Henrik’s solution works directly on the form rather than the specific means of submitting it. That takes care of submit buttons, images, enter keys, scripts, or any other method of submitting the form. Thanks for the help, both of you.
about 4 months ago
What about hitting enter twice?
about 2 months ago
Why not just limit to one submit? This solves hitting enter twice as Skylar said.
var i = 0;
$(“form”).submit(function()
{
i++;
if ( i > 1 ) { return false; }
});
about 1 week ago
@henrik.. good one