简体   繁体   English

单击后立即删除Click事件处理程序

[英]Remove click event handler immediately after click

I've been trying to remove the click event handler after being clicked once to prevent the event from firing multiple times. 我一直在尝试单击一次后删除click事件处理程序,以防止多次触发该事件。

I've tried .unbind() , .off() , .one() and put each in various different places in the function, but none seem to work. 我试过.unbind() .off() .one()并把每一个在各个不同的地方的功能,但没有似乎工作。

If I use .off() I get a undefined is not a function error. 如果我使用.off() ,则得到的undefined is not a function错误。

If I use .one() I want to basically turn the event back on if I click the other button. 如果我使用.one()我想在单击其他按钮时基本上重新打开该事件。 So if I click on select, I want it to only fire once, but if I then click on unselect, I want select to be clickable again. 因此,如果单击“选择”,则希望它仅触发一次,但是如果单击“取消选择”,则希望再次单击。

I've tried doing the following, but oddly the code doesn't even fire, and so I've been forced to use click() . 我尝试执行以下操作,但是奇怪的是,该代码甚至都没有触发,因此我不得不使用click()

$().on('click', function()...

Which function should I use and where should I put it? 我应该使用哪个功能,应该放在哪里?

(function($) {

Drupal.behaviors.wysiwyg = {
    attach: function(context, settings) {

        var toggle = function(state) {
            $('#wysiwyg-profile-form .buttons-and-plugins input:checkbox').attr('checked', state);
        }

        $('#wysiwyg-select-all').click(function(){
    toggle(true);
            return false;
        });

        $('#wysiwyg-unselect-all').click(function(){
    toggle(false);
            return false;
        });
    }
}

}(jQuery))

UPDATED 更新

I was messing around with a couple of the things these other guys were suggesting and I think this is exactly what you need to accomplish this: 我正在搞弄其他家伙在暗示的一些事情,我想这正是您需要实现的目标:

(function ($) {

    Drupal.behaviors.wysiwyg = {
        attach: function (context, settings) {

            var selectFn = function() {
                toggle(true);
                return false;
            }

            var unselectFn = function() {
                toggle(false);
                return false;
            }    

            var toggle = function (state) {
                $('#wysiwyg-profile-form .buttons-and-plugins input:checkbox').attr('checked', state);
                if (state) {
                    $(document).off("click", '#wysiwyg-select-all,#wysiwyg-unselect-all', "**");
                    $(document).on("click", "#wysiwyg-unselect-all", unselectFn;
                }else{
                    $(document).off("click", '#wysiwyg-select-all,#wysiwyg-unselect-all', "**");
                    $(document).on("click", "#wysiwyg-select-all", selectFn;
                }
            };

            $(document).on("click", "#wysiwyg-select-all", selectFn);
            $(document).on("click", "#wysiwyg-unselect-all", unselectFn);

        }
    };

}(jQuery));

It toggles the binding of the buttons events and will work with dynamically created elements. 它切换按钮事件的绑定,并将与动态创建的元素一起使用。 This is a derivative of Matt Green's answer, but I noticed several problems with his, namely that he was calling the selectFn and unselectFn in his event binding rather than referencing them. 这是马特·格林(Matt Green)回答的衍生产品,但我注意到他的几个问题,即他在事件绑定中调用了selectFn和unselectFn而不是引用它们。

Check if this works for you. 检查是否适合您。 Also make sure the jquery version is above 1.7 for "on", "off" to work. 另外,请确保jquery版本高于1.7,“打开”,“关闭”才能正常工作。

(function($) {

  Drupal.behaviors.wysiwyg = {
    attach: function(context, settings) {
    var counter = 0;
      var toggle = function(state) {
        $('#wysiwyg-profile-form .buttons-and-plugins input:checkbox').attr('checked', state);
      }

      $(document).off('click',"#wysiwyg-select-all").on('click', "#wysiwyg-select-all", function(e) {
        toggle(true);               
        return false;
      });

      $(document).off('click',"#wysiwyg-unselect-all").on('click', "#wysiwyg-unselect-all", function(e) {
        toggle(false);      
        return false;
      });
    }
  }

}(jQuery))

Something like this: 像这样:

$("#wysiwyg-select-all").one("click", function() {
    toggle(true);
    return false;
});

Add a class name to the click button and then remove the class once its clicked once. 将类名称添加到单击按钮,然后在单击一次该类后将其删除。

$('#wysiwyg-select-all .myclassname').on('click', function(){
    toggle(true);
    $(this).removeClass('myclassname');
    $('#wysiwyg-unselect-all').addClass('secondclassname');
    return false;
});

$('#wysiwyg-unselect-all .secondclassname').on('click', function(){
    toggle(false);
    $('#wysiwyg-select-all').addClass('myclassname');
    $(this).removeClass('secondclassname');
    return false;
});

This should work for dynamically created elements: 这应该适用于动态创建的元素:

(function ($) {

    Drupal.behaviors.wysiwyg = {
        attach: function (context, settings) {

            var toggle = function (state) {
                $('#wysiwyg-profile-form .buttons-and-plugins input:checkbox').attr('checked', state);
            };

            $(document).on("click", "#wysiwyg-select-all", selectFn());
            $(document).on("click", "#wysiwyg-unselect-all", unselectFn());
        }
    };

}(jQuery));


function selectFn() {
    $(document).off("click", '#wysiwyg-select-all', "**");
    $(document).on("click", "#wysiwyg-unselect-all", unselectFn());
    toggle(true);
    return false;
}

function unselectFn() {
    $(document).off("click", '#wysiwyg-unselect-all', "**");
    $(document).on("click", "#wysiwyg-select-all", selectFn());
    toggle(false);
    return false;
}

Why don't you use any global variable as a flag. 为什么不使用任何全局变量作为标志。 On click event set it true. 点击事件设置为true。

if (btn1Clicked)
     return;
btn1Clicked=true; 

And to reenable just set it false. 要重新启用,只需将其设置为false。 You can use return false instead of return if required. 您可以根据需要使用return false而不是return。

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

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