How to temporarily disable a click handler in jQuery?

前端 未结 12 1991
不思量自难忘°
不思量自难忘° 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条回答
  •  -上瘾入骨i
    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
        }
       );
    

提交回复
热议问题