How to temporarily disable a click handler in jQuery?

前端 未结 12 1993
不思量自难忘°
不思量自难忘° 2020-12-04 14:02

Say I have something like the following to trap the click event of a button:

$(\"#button_id\").click(function() {
  //disable click event
  //do something
           


        
相关标签:
12条回答
  • 2020-12-04 14:44

    You can do it like the other people before me told you using a look:

    A.) Use .data of the button element to share a look variable (or a just global variable)

    if ($('#buttonId').data('locked') == 1)
        return
    $('#buttonId').data('locked') = 1;
    // Do your thing
    $('#buttonId').data('locked') = 0;
    

    B.) Disable mouse signals

    $("#buttonId").css("pointer-events", "none");
    // Do your thing
    $("#buttonId").css("pointer-events", "auto");
    

    C.) If it is a HTML button you can disable it (input [type=submit] or button)

    $("#buttonId").attr("disabled", "true");
    // Do your thing
    $("#buttonId").attr("disabled", "false");
    

    But watch out for other threads! I failed many times because my animation (fading in or out) took one second. E.g. fadeIn/fadeOut supports a callback function as second parameter. If there is no other way just do it using setTimeout(callback, delay).

    Greets, Thomas

    0 讨论(0)
  • 2020-12-04 14:44

    it is better that use current event and dont save handler in global handler. i get current element event then unbind then bind again. for a handler.

    var element =  $("#elemid")[0];
    var tempHandler = jQuery._data(element)["events"]["click"][0].handler;
    $("#elemid").unbind("click");
    
    // do the job that click not suppose to listen;
    $("#elemid").bind("click" , tempHandler );
    

    for all handler

    var element =  $("#elemid")[0];
    var clickHandlerList = jQuery._data(element)["events"]["click"];
    var handlerList = [];
    for(var i  = 0 ; i <  clickHandlerList .length ; i++) {
        handlerList .push(clickHandlerList [i].handler);
    }
    $("#elemid").unbind("click");
    // do the job that click not suppose to listen;
    for(var i  = 0 ; i <  handlerList.length ; i++) {
        // return back all handler to element.
        $("#elemid").bind("click" , handlerList[i]);
    }
    
    0 讨论(0)
  • 2020-12-04 14:48

    I noticed this post was old but it appears top on google and this kind of solution was never offered so I decided to post it anyway.

    You can just disable cursor-events and enable them again later via css. It is supported on all major browsers and may prove useful in some situations.

    $("#button_id").click(function() {
    
       $("#button_id").css("pointer-events", "none");
       //do something
       $("#button_id").css("pointer-events", "auto");
    }
    
    0 讨论(0)
  • 2020-12-04 14:50
    $("#button_id").click(function() {
        $('#button_id').attr('disabled', 'true');
        $('#myDiv').hide(function() { $('#button_id').removeAttr('disabled'); });
    }); 
    

    Don't use .attr() to do the disabled, use .prop(), it's better.

    0 讨论(0)
  • 2020-12-04 14:52

    If #button_id implies a standard HTML button (like a submit button) you can use the 'disabled' attribute to make the button inactive to the browser.

    $("#button_id").click(function() {
        $('#button_id').attr('disabled', 'true');
    
        //do something
    
         $('#button_id').removeAttr('disabled');
    });   
    

    What you may need to be careful with, however, is the order in which these things may happen. If you are using the jquery hide command, you may want to include the "$('#button_id').removeAttr('disabled');" as part of a call back, so that it does not happen until the hide is complete.

    [edit] example of function using a callback:

    $("#button_id").click(function() {
        $('#button_id').attr('disabled', 'true');
        $('#myDiv').hide(function() { $('#button_id').removeAttr('disabled'); });
    });   
    
    0 讨论(0)
  • 2020-12-04 14:54

    This code will display loading on the button label, and set button to disable state, then after processing, re-enable and return back the original button text:

    $(function () {
    
            $(".btn-Loading").each(function (idx, elm) {
                $(elm).click(function () {
                    //do processing
                    if ($(".input-validation-error").length > 0)
                        return;
                    $(this).attr("label", $(this).text()).text("loading ....");
                    $(this).delay(1000).animate({ disabled: true }, 1000, function () {
                        //original event call
                        $.when($(elm).delay(1000).one("click")).done(function () {
                            $(this).animate({ disabled: false }, 1000, function () {
                                $(this).text($(this).attr("label"));
                            })
                        });
                        //processing finalized
                    });
                });
            });
            // and fire it after definition
        }
       );
    
    0 讨论(0)
提交回复
热议问题