Created
March 13, 2014 23:59
-
-
Save Reyjay/9539633 to your computer and use it in GitHub Desktop.
Prevent button from being double clicked on a form
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
<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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good job