SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Form Fails to Submit After Disabling Button

  1. #1
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,699
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Form Fails to Submit After Disabling Button

    I'm attempting to disable a submit button onclick to prevent the user from double-clicking and submitting the same data twice. Seems simple enough but once I add in my disable code (as seen below) the button successfully is disabled but the form fails to submit.

    HTML Code:
    <button type="submit" class="btn" onclick="disableButtons();">Submit</button>
    HTML Code:
    <script type="text/javascript">
        function disableButtons() {
            $(".btn").each(function () {
                $(this).attr("disabled", "disabled");
            });
        }
    </script>
    Any ideas what I'm doing wrong here?
    TAKE A WALK OUTSIDE YOUR MIND.

  2. #2
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    Try this

    Code JavaScript:
    function disableButtons() {
        $('.btn').each(function () {
            $(this).attr('disabled', 'disabled');
            $(this).parents('form').submit();
        });
    }
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,699
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you kindly, Sir. Worked great!
    TAKE A WALK OUTSIDE YOUR MIND.

  4. #4
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    3,665
    Mentioned
    21 Post(s)
    Tagged
    1 Thread(s)
    You should probably use onsubmit, rather than onclick. Onclick is triggered any time the button is clicked, which doesn't necessarily mean that the form will be submitted. Onsubmit, on the other hand, is triggered when the submit event actually occurs.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •