Skip to content

Instantly share code, notes, and snippets.

@davetayls
Created March 5, 2013 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetayls/5091528 to your computer and use it in GitHub Desktop.
Save davetayls/5091528 to your computer and use it in GitHub Desktop.
jquery.preventDoubleSubmit
/**
* A jquery plugin to prevent a form being submitted twice
*
* You'd use this on the form itself
* $('form').preventDoubleSubmit();
*/
;(function($) {
function submitHandler(e) {
var $form = $(this);
if ($form.data('submitted') === true) {
// Previously submitted - don't submit again
return false;
} else {
// Mark it so that the next submit can be ignored
$form.data('submitted', true).addClass('form-submitted');
}
}
$.fn.preventDoubleSubmit = function() {
return this
.unbind('submit', submitHandler)
.bind('submit', submitHandler)
;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment