Created
March 5, 2013 16:23
-
-
Save davetayls/5091528 to your computer and use it in GitHub Desktop.
jquery.preventDoubleSubmit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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