Preventing multiple clicks on button

后端 未结 14 1965
小鲜肉
小鲜肉 2020-12-02 09:43

I have following jQuery code to prevent double clicking a button. It works fine. I am using Page_ClientValidate() to ensure that the double click is prevented o

14条回答
  •  再見小時候
    2020-12-02 10:33

    disable the button on click, enable it after the operation completes

    $(document).ready(function () {
        $("#btn").on("click", function() {
            $(this).attr("disabled", "disabled");
            doWork(); //this method contains your logic
        });
    });
    
    function doWork() {
        alert("doing work");
        //actually this function will do something and when processing is done the button is enabled by removing the 'disabled' attribute
        //I use setTimeout so you can see the button can only be clicked once, and can't be clicked again while work is being done
        setTimeout('$("#btn").removeAttr("disabled")', 1500);
    }
    

    working example

提交回复
热议问题