Why doesn't my form post when I disable the submit button to prevent double clicking?

后端 未结 10 1263
广开言路
广开言路 2020-12-31 19:06

Like every other web developer on the planet, I have an issue with users double clicking the submit button on my forms. My understanding is that the conventional way to han

相关标签:
10条回答
  • 2020-12-31 19:30

    "Disabling" HTML controls doesn't always produce consistent behavior in all major browsers. So I try to stay away from doing that on the client-side, because (working with the ASP.NET model) you need to keep track of element's state on client and server in that case.

    What I'd do is move button off the visible part of the window by switching the button's className to a CSS class that contains the following:

    .hiddenButton
    {
      position: absolute;
      top: -1000px;
      left: -1000px;
    }
    

    Now, what to put in place of the button?

    1. Either an image that looks like a disabled button
    2. Or just plain text that says "Please wait..."

    And this can be done the same way but in reverse. Start with the element being hidden at page load and then switch to a visible className on form submit.

    0 讨论(0)
  • 2020-12-31 19:41

    UseSubmitBehavior="false" converts submit button to normal button (<input type="button">). If you don't want this to happen, you can hide submit button and immediately insert disabled button on its place. Because this happens so quickly it will look as button becoming disabled to user. Details are at the blog of Josh Stodola.

    Code example (jQuery):

    $("#<%= btnSubmit.ClientID %>").click(function()
    {
      $(this)
        .hide()
        .after('<input type="button" value="Please Wait..." disabled="disabled" />');
    });
    
    0 讨论(0)
  • 2020-12-31 19:41

    FOR JQUERY USERS

    You will get into all sorts of problems trying to add javascript directly to the onClick event on ASP.NET buttons when using jQuery event listeners.

    I found the best way to disable buttons and get the postback to work was to do something like this:

        $(buttonID).bind('click', function (e) {
    
            if (ValidateForm(e)) {
    
                //client side validation ok!
                //disable the button:
                $(buttonID).attr("disabled", true);
    
                //force a postback:
                try {
                    __doPostBack($(buttonID).attr("name"), "");
                    return true;
                } catch (err) {
                    return true;
                }
    
            }
            //client side validation failed!
            return false;
        });
    

    Where ValidateForm is your custom validation function which returns true or false if your form validates client side.

    And buttonID is the id of your button such as '#button1'

    0 讨论(0)
  • 2020-12-31 19:46

    fallen888 is right, your approach doesn't work cross-browser. I use this little snippet to prevent double-click.

    0 讨论(0)
  • 2020-12-31 19:46

    For debugging purposes, what happens if you put an else clause against the if(chk.checked)?

    0 讨论(0)
  • 2020-12-31 19:47

    I think you're just missing this tag:

    UseSubmitBehavior="false"
    

    Try it like this:

    <asp:Button ID="btnUpdate" runat="server" UseSubmitBehavior="false" OnClientClick="if(Page_ClientValidate()) { this.disabled = true; } else {return false;}" Text = "Update" CssClass="button" OnClick="btnUpdate_Click" ValidationGroup="vgNew"/>
    

    Explanation

    0 讨论(0)
提交回复
热议问题