Alex Rivera | Logout

What is the best approach for (client-side) disabling of a submit button?

Asked 2008-09-05T15:45:22.713
11

Details:

  • Only disable after user clicks the submit button, but before the posting back to the server
  • ASP.NET Webforms (.NET 1.1)
  • Prefer jQuery (if any library at all)
  • Must be enabled if form reloads (i.e. credit card failed)

This isn't a necessity that I do this, but if there is a simple way to do it without having to change too much, I'll do it. (i.e. if there isn't a simple solution, I probably won't do it, so don't worry about digging too deep)

Edit
Report

1 Answer

8

You could do something like this:

$('form').submit(function() {
    $(this)
        .find(":submit,:image")             // get all the submit buttons
        .attr({ disabled : 'disabled' })    // disable them
        .end()                              // go back to this form
        .submit(function() {                // change the onsubmit to always reject.
            return false;
        })
    ;
});

Benefits of this:

  • It will work with all your forms, with all methods of submission:
    • clicking a submit element
    • pressing enter, or
    • calling form.submit() from some other code
  • It will disable all submit elements:
    • <input type="submit"/>
    • <button type="submit"></button>
    • <input type="image" />
  • it's really short.
answered 2008-10-30T07:57:29.687

Your Answer