Skip to content

Instantly share code, notes, and snippets.

@bq1990
Last active October 28, 2015 01:18
Show Gist options
  • Save bq1990/098fc8e30a4ec94b58d0 to your computer and use it in GitHub Desktop.
Save bq1990/098fc8e30a4ec94b58d0 to your computer and use it in GitHub Desktop.
Prevent double submit
//from http://stackoverflow.com/questions/2830542/prevent-double-submission-of-forms-in-jquery
//usage: $('form').preventDoubleSubmission();
//disable per form: $('form:not(.js-allow-double-submission)').preventDoubleSubmission();
jQuery.fn.preventDoubleSubmission = function() {
$(this).on('submit',function(e){
var $form = $(this);
if ($form.data('submitted') === true) {
// Previously submitted - don't submit again
e.preventDefault();
} else {
// Mark it so that the next submit can be ignored
$form.data('submitted', true);
}
});
// Keep chainability
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment