简体   繁体   English

禁用/启用jQuery onClick函数

[英]Disabling/Enabling Jquery onClick Functions

Is there any way to 'disable' or 'suspend' a jquery .click function, without unbinding? 有什么办法可以“禁用”或“暂停” jquery .click函数,而无需解除绑定? I want to leave the function bound, but don't want it to fire. 我想让函数绑定,但不希望它触发。 Then, at a later time, I would like to re-enable it. 然后,稍后,我想重新启用它。

Just disabling the control being bound to will not suffice in this case. 在这种情况下,仅禁用绑定的控件是不够的。

You could set a flag in a higher variable scope and only execute the handler if the flag is on. 您可以在较高的变量范围内设置标志,并且仅在标志打开时才执行处理程序。 Here's a simple example: 这是一个简单的例子:

$(document).ready(function () {
    var execHandler = false;
    $('input').click(function (e) {
        if (execHandler) {
            //do stuff
        }
        e.preventDefault();
        return false;
    });
    $('button#toggleHandler').click(function (e) {
        execHandler = !execHandler;
        e.preventDefault();
        return false;
    })
});

It's a similiar solution to: Suppress jQuery event handling temporarily 这是一个类似的解决方案: 暂时禁止jQuery事件处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM