How to prevent a double-click using jQuery?

前端 未结 15 788
孤街浪徒
孤街浪徒 2020-11-30 23:42

I have a button as such:


Within jQuery I am using the following, but it still

相关标签:
15条回答
  • 2020-12-01 00:19

    In my case, jQuery one had some side effects (in IE8) and I ended up using the following :

    $(document).ready(function(){
      $("*").dblclick(function(e){
        e.preventDefault();
      });
    });
    

    which works very nicely and looks simpler. Found it there: http://www.jquerybyexample.net/2013/01/disable-mouse-double-click-using-javascript-or-jquery.html

    0 讨论(0)
  • 2020-12-01 00:19
    /*
    Double click behaves as one single click
    
    
    "It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable."
    
    That way we have to check what is the event that is being executed at any sequence. 
    
       */
           var totalClicks = 1;
    
     $('#elementId').on('click dblclick', function (e) {
    
     if (e.type == "dblclick") {
        console.log("e.type1: " + e.type);
        return;
     } else if (e.type == "click") {
    
        if (totalClicks > 1) {
            console.log("e.type2: " + e.type);
            totalClicks = 1;
            return;
        } else {
            console.log("e.type3: " + e.type);
            ++totalClicks;
        }
    
        //execute the code you want to execute
    }
    
    });
    
    0 讨论(0)
  • 2020-12-01 00:19

    You can do something like this to disable submit button when loading a new page with the same form:

    $("#btnConfirm").bind("click", function (e) {
      $("#btnConfirm ").css("pointer-events", "none");
    });
    

    The submit button could look like this:

    <input
        type="submit"
        name="ACMS_POST_Form_Confirm"
        value="confirm"
        id="btnConfirm"
    />
    
    0 讨论(0)
  • 2020-12-01 00:20

    I found that most solutions didn't work with clicks on elements like Labels or DIV's (eg. when using Kendo controls). So I made this simple solution:

    function isDoubleClicked(element) {
        //if already clicked return TRUE to indicate this click is not allowed
        if (element.data("isclicked")) return true;
    
        //mark as clicked for 1 second
        element.data("isclicked", true);
        setTimeout(function () {
            element.removeData("isclicked");
        }, 1000);
    
        //return FALSE to indicate this click was allowed
        return false;
    }
    

    Use it on the place where you have to decide to start an event or not:

    $('#button').on("click", function () {
        if (isDoubleClicked($(this))) return;
    
        ..continue...
    });
    
    0 讨论(0)
  • 2020-12-01 00:22

    If what you really want is to avoid multiple form submissions, and not just prevent double click, using jQuery one() on a button's click event can be problematic if there's client-side validation (such as text fields marked as required). That's because click triggers client-side validation, and if the validation fails you cannot use the button again. To avoid this, one() can still be used directly on the form's submit event. This is the cleanest jQuery-based solution I found for that:

    <script type="text/javascript">
    $("#my-signup-form").one("submit", function() {
        // Just disable the button.
        // There will be only one form submission.
        $("#my-signup-btn").prop("disabled", true);
    });
    </script>
    
    0 讨论(0)
  • 2020-12-01 00:26

    My solution: https://gist.github.com/pangui/86b5e0610b53ddf28f94 It prevents double click but accepts more clicks after 1 second. Hope it helps.

    Here is the code:

    jQuery.fn.preventDoubleClick = function() {
      $(this).on('click', function(e){
        var $el = $(this);
        if($el.data('clicked')){
          // Previously clicked, stop actions
          e.preventDefault();
          e.stopPropagation();
        }else{
          // Mark to ignore next click
          $el.data('clicked', true);
          // Unmark after 1 second
          window.setTimeout(function(){
            $el.removeData('clicked');
          }, 1000)
        }
      });
      return this;
    }; 
    
    0 讨论(0)
提交回复
热议问题