Disabling links to stop double-clicks in JQuery

前端 未结 16 742
别跟我提以往
别跟我提以往 2020-12-02 11:13

How would I disable all links with the button class after they are clicked once? I\'d like to be able to do this in one place, and not have to change all of the

相关标签:
16条回答
  • 2020-12-02 11:55

    Yeah, set a value to keep track of this:

    $("a").click(function (){
      if( $(this).data("clicked") ){
        return false;
      }
      $(this).data("clicked", true);
      return true;
    });
    
    0 讨论(0)
  • 2020-12-02 11:57

    This works for both IE and Chrome with href and onclick attributes on anchor tags:

    $(document).ready(
            function()
            {          
              $('a.button').on("click",
                function(event)
                {
                  $('a.button').prop('onclick',null);
                  $('a.button').removeAttr('href');
                });
            });

    0 讨论(0)
  • 2020-12-02 12:00

    I like the idea of solution provided by Adam, however this didn't work for me. If you're having trouble using .one() to disable links after click, I recommend trying the following.

    $("a.button").bind("click", function( event ) {
        // Unbind the click event from anchors with class button
        $(this).unbind( event );
    });
    
    0 讨论(0)
  • 2020-12-02 12:02

    This is what I would try:

    $("a.button").one("click", function() {
        $(this).click(function () { return false; });
    });
    

    Bind all the links with class "button". Run the anonymous function only once (hence "one"), which rebinds the link to return false.

    If you want to keep it looking more like what you have, try this:

    $("a.button").click(function() { $(this).attr("disabled", "disabled"); });
    $(document).click(function(evt) {
         if ($(evt.target).is("a[disabled]"))
              return false;
    });
    
    0 讨论(0)
  • 2020-12-02 12:03

    Events are only bound on document.ready, you should handle this with a .live event:

    $("a.button").live("click", function() { $(this).attr("disabled", "disabled"); });
    $("a[disabled]").live("click", function() { return false; });
    
    0 讨论(0)
  • 2020-12-02 12:03

    This should work when you are using a function

    var clickRunning = false;
    function OpenSth(){
         if (clickRunning) return;
         clickRunning = true;
    
         yourFunc({
          /* do your stuff*/
         },500,  function () {
            clickRunning= false;
         });
    }
    
    0 讨论(0)
提交回复
热议问题