jquery disable submit button on form submission

scarhand picture scarhand · Mar 26, 2011 · Viewed 53k times · Source

For whatever reason, though this code does refresh the page, no fields get posted...

$('form').submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
});

Is there a better way of coding this?

Answer

Diodeus - James MacFarlane picture Diodeus - James MacFarlane · Mar 26, 2011

Your code is changing the submit action of the form. Instead of submitting, it changes the button attribute.

Try this:

$('input[type=submit]').click(function() {
    $(this).attr('disabled', 'disabled');
    $(this).parents('form').submit();
});