Disable button on form submission

前端 未结 17 1549
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 00:13

I have a button that I would like to disable when the form submits to prevent the user submitting multiple times.

I have tried naively disabling the button with java

相关标签:
17条回答
  • 2020-12-01 00:51

    The correct (as far as user-friendliness is concerned, at least) way would be to disable the button using the OnClientClick attribute, perform the client-side validation, and then use the result of that to continue or re-enable the button.

    Of course, you should ALSO write server-side code for this, as you cannot rely on the validation even being carried out due to a lack, or particular implementation, of JavaScript. However, if you rely on the server controlling the button's enabled / disabled state, then you basically have no way of blocking the user submitting the form multiple times anyway. For this reason you should have some kind of logic to detect multiple submissions from the same user in a short time period (identical values from the same Session, for example).

    0 讨论(0)
  • 2020-12-01 00:52

    This is an easier but similar method than what rp has suggested:

    function submit(button) {
            Page_ClientValidate(); 
            if(Page_IsValid)
            {
                button.disabled = true;
            }
    }
    
     <asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_OnClick" OnClientClick="submit(this)" Text="Submit Me" />
    
    0 讨论(0)
  • 2020-12-01 00:55

    I'm not a huge fan of writing all that javascript in the code-behind. Here is what my final solution looks like.

    Button:

    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" OnClientClick="doSubmit(this)" />
    

    Javascript:

    <script type="text/javascript"><!--
    function doSubmit(btnSubmit) {
        if (typeof(Page_ClientValidate) == 'function' && Page_ClientValidate() == false) { 
            return false;
        }    
        btnSubmit.disabled = 'disabled';
        btnSubmit.value = 'Processing. This may take several minutes...';
        <%= ClientScript.GetPostBackEventReference(btnSubmit, string.Empty) %>;    
    }
    //-->
    </script>
    
    0 讨论(0)
  • 2020-12-01 00:56

    Building on @rp.'s answer, I modified it to invoke the custom function and either submit and disable on success or "halt" on error:

    public static void DisableButtonOnClick(Button ButtonControl, string ClientFunction)
    {
        StringBuilder sb = new StringBuilder(128);
    
        if (!String.IsNullOrEmpty(ClientFunction))
        {
            sb.AppendFormat("if (typeof({0}) == 'function') {{ if ({0}()) {{ {1}; this.disabled=true; return true; }} else {{ return false; }} }};", ClientFunction, ButtonControl.Page.ClientScript.GetPostBackEventReference(ButtonControl, null));
        }
        else
        {
            sb.Append("return true;");
        }
    
        ButtonControl.Attributes.Add("onclick", sb.ToString());
    }
    
    0 讨论(0)
  • 2020-12-01 01:01

    if the validation is successful, then disable the button. if it's not, then don't.

    function validate(form) {
      // perform validation here
      if (isValid) {
        form.mySubmitButton.disabled = true;
        return true;
      } else {
        return false;
      }
    }
    
    <form onsubmit="return validate(this);">...</form>
    
    0 讨论(0)
  • 2020-12-01 01:01

    A solution will be to set a hidden field when the button is clicked, with the number 1.

    On the button click handler first thing is to check that number if it is something other than 1 just return out of the function.

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