Skip to content

Instantly share code, notes, and snippets.

@Reyjay
Created March 13, 2014 23:59
Show Gist options
  • Save Reyjay/9539633 to your computer and use it in GitHub Desktop.
Save Reyjay/9539633 to your computer and use it in GitHub Desktop.
Prevent button from being double clicked on a form
<script type="text/javascript">
// jQuery plugin to prevent double submission of forms
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();
alert('prevented');
} else {
// Mark it so that the next submit can be ignored
$form.data('submitted', true);
alert('submited');
}
});
// Keep chainability
return this;
};
$('##shippingFormContent').preventDoubleSubmission();
$('##addToCartForm').preventDoubleSubmission();
</script>
// jQuery plugin to prevent double submission of forms
jQuery.fn.preventDoubleSubmit = function() {
$(this).submit(function (e) {
var theForm = $(this);
setTimeout(function () {
var btn = theForm.find('input[type="submit"]');
btn.attr('disabled', 'disabled');
btn.val('Processing');
}, 5);
return true;
});
};
$('##addToCartForm').preventDoubleSubmit();
@melfatih90
Copy link

good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment