Disable button on form submission

前端 未结 17 1685
伪装坚强ぢ
伪装坚强ぢ 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 01:10

    Just heard about the "DisableOnSubmit" property of an , like so:

    
    

    When rendered, the button's onclick attribute looks like so:

    onclick="this.disabled=true; setTimeout('enableBack()', 3000);
      WebForm_DoPostBackWithOptions(new
      WebForm_PostBackOptions('yourControlsName', '', true, '', '', false, true))
    

    And the "enableBack()' javascript function looks like this:

    function enableBack()
    {
        document.getElementById('yourControlsName').disabled=false;
    }
    

    So when the button is clicked, it becomes disabled for 3 seconds. If the form posts successfully then you never see the button re-enable. If, however, any validators fail then the button becomes enabled again after 3 seconds.

    All this just by setting an attribute on the button--no javascript code needs to be written by hand.

提交回复
热议问题