Preventing multiple clicks on button

后端 未结 14 1900
小鲜肉
小鲜肉 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:28

    using count,

     clickcount++;
        if (clickcount == 1) {}
    

    After coming back again clickcount set to zero.

    0 讨论(0)
  • 2020-12-02 10:30

    JS provides an easy solution by using the event properties:

    $('selector').click(function(event) {
      if(!event.detail || event.detail == 1){//activate on first click only to avoid hiding again on multiple clicks
        // code here. // It will execute only once on multiple clicks
      }
    });
    
    0 讨论(0)
  • 2020-12-02 10:31

    I modified the solution by @Kalyani and so far it's been working beautifully!

    $('selector').click(function(event) {
      if(!event.detail || event.detail == 1){ return true; }
      else { return false; }
    });
    
    0 讨论(0)
  • 2020-12-02 10:31

    We can use on and off click for preventing Multiple clicks. i tried it to my application and it's working as expected.

    $(document).ready(function () {     
        $("#disable").on('click', function () {
            $(this).off('click'); 
            // enter code here
        });
    })
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 10:34

    I found this solution that is simple and worked for me:

    <form ...>
    <input ...>
    <button ... onclick="this.disabled=true;this.value='Submitting...'; this.form.submit();">
    </form>
    

    This solution was found in: Original solution

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