Skip to content

Instantly share code, notes, and snippets.

@slickplaid
Created March 12, 2012 13:09
Show Gist options
  • Save slickplaid/2021780 to your computer and use it in GitHub Desktop.
Save slickplaid/2021780 to your computer and use it in GitHub Desktop.
Stop double submission
var formSubmitted = false;
$(document).on('submit', '.form', function(e) {
if(formSubmitted) {
e.preventDefault();
} else {
formSubmitted = true;
}
});
var formSubmitted = false;
$(document).on('click', '.info-section input[value="save"]', function(e) {
if(formSubmitted) {
e.preventDefault();
} else {
formSubmitted = true;
}
});
$(document).on('click', 'input[type=submit]', function(e) {
var $this = $(this);
console.log('clicked');
// catch ajax event, so regular input buttons still work w/o ajax
$(document).one('ajaxStart', function(ev) {
e.preventDefault(); // stop default submit action if it hasn't already been stopped
$this.attr('disabled', true); // disable the button
console.log('disabled');
});
$(document).one('ajaxComplete', function(ev) {
$this.attr('disabled', false); // re-enable the button
console.log('re-enabled');
});
});
$(document).on('click', 'input[type=submit]', function(){
$(this).click(function() {
return false;
});
});
(function() {
var submit = false;
$(document).on('submit', 'form', function(e) {
if(submit) {
e.preventDefault();
} else {
submit = true;
}
});
})();
$(document).on('submit', 'form', function(e) {
var submit = false;
$(this).submit(function(ev) {
if(submit) {
ev.preventDefault();
} else {
submit = true;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment