jQuery多事件处理程序 - 如何取消?

时间:2023-01-23 00:04:01

I have two functions bound to a click event at two different times (using jQuery). The order in which they are fired is significant. They are firing in the correct order. The problem is, when the first function returns false, the second function is still firing!

我有两个函数绑定到两个不同时间的单击事件(使用jQuery)。他们被解雇的顺序很重要。他们按照正确的顺序开火。问题是,当第一个函数返回false时,第二个函数仍在触发!

How can I properly cancel the event?

如何正确取消活动?

Example code:

示例代码:

$(document).click(function() { 
  alert('a');
  return false;
});

$(document).click(function() {
  alert('b');
});

You will still see the "b" alert message when clicking the page. This is unacceptable!

单击页面时,您仍会看到“b”警告消息。这是无法接受的!

5 个解决方案

#1


82  

Use the stopImmediatePropagation function of the jQuery event object.

使用jQuery事件对象的stopImmediatePropagation函数。

Keeps the rest of the handlers from being executed. This method also stops the bubbling by calling event.stopPropagation().

使其余的处理程序不被执行。此方法还通过调用event.stopPropagation()来停止冒泡。

$(document).click(function(event) { 
  alert('a');
  event.stopImmediatePropagation();
  return false;
});

$(document).click(function() {
  alert('b');
});

#2


2  

The first thing I'm asking is: why do you have two functions bound to the same click event? Having access to the code, why don't you just make that one single call?

我要问的第一件事是:为什么你有两个函数绑定到同一个click事件?有权访问代码,为什么不只进行一次调用?

$(function (){
    var callbackOne = function(e){
        alert("I'm the first callback... Warning, I might return false!");
        return false;
    };

    var callbackTwo = function(e){
        alert("I'm the second callback");
    };

    $(document).click(function (e){
        if(callbackOne(e)){
            callbackTwo(e);
        }
    });
});

#3


2  

Thanks, unbind works for redefining functions.

谢谢,unbind用于重新定义功能。

  $(document).ready(function(){
        $("#buscar_mercaderia").click(function(){ alert('Seleccione Tipo de mercaderia'); 
   });
$(".tipo").live('click',function(){
            if($(this).attr('checked')!=''){
                if($(this).val()=='libro'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){  window.open('buscar_libro.php','Buscar Libros', 'width=800,height=500'); });
                }else if($(this).val()=='otra'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){ window.open('buscar_mercaderia.php','Buscar Mercaderias', 'width=800,height=500');  });
                }
            }
        })

#4


1  

Does using unbind help?

使用unbind有帮助吗?

$(document).click(function() { 
  alert('a');
  $(this).unbind('click');
  return false;
});

#5


0  

If what nickf said doesn't work, you could use a small state machine:

如果说诺夫说不起作用,你可以使用一个小型状态机:

var trigger = 0;

$(document).click(function() { 

  alert('a');
  if(you_want_to_return_false) {
    trigger = 1;
    return false;
  }
});

$(document).click(function() {
  if(trigger !== 0) {
  alert('b');
  } 
  trigger = 0;
});

Not the prettiest solution, but it will work.

不是最漂亮的解决方案,但它会起作用。

#1


82  

Use the stopImmediatePropagation function of the jQuery event object.

使用jQuery事件对象的stopImmediatePropagation函数。

Keeps the rest of the handlers from being executed. This method also stops the bubbling by calling event.stopPropagation().

使其余的处理程序不被执行。此方法还通过调用event.stopPropagation()来停止冒泡。

$(document).click(function(event) { 
  alert('a');
  event.stopImmediatePropagation();
  return false;
});

$(document).click(function() {
  alert('b');
});

#2


2  

The first thing I'm asking is: why do you have two functions bound to the same click event? Having access to the code, why don't you just make that one single call?

我要问的第一件事是:为什么你有两个函数绑定到同一个click事件?有权访问代码,为什么不只进行一次调用?

$(function (){
    var callbackOne = function(e){
        alert("I'm the first callback... Warning, I might return false!");
        return false;
    };

    var callbackTwo = function(e){
        alert("I'm the second callback");
    };

    $(document).click(function (e){
        if(callbackOne(e)){
            callbackTwo(e);
        }
    });
});

#3


2  

Thanks, unbind works for redefining functions.

谢谢,unbind用于重新定义功能。

  $(document).ready(function(){
        $("#buscar_mercaderia").click(function(){ alert('Seleccione Tipo de mercaderia'); 
   });
$(".tipo").live('click',function(){
            if($(this).attr('checked')!=''){
                if($(this).val()=='libro'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){  window.open('buscar_libro.php','Buscar Libros', 'width=800,height=500'); });
                }else if($(this).val()=='otra'){
                    $("#buscar_mercaderia").unbind('click');
                    $("#buscar_mercaderia").click(function(){ window.open('buscar_mercaderia.php','Buscar Mercaderias', 'width=800,height=500');  });
                }
            }
        })

#4


1  

Does using unbind help?

使用unbind有帮助吗?

$(document).click(function() { 
  alert('a');
  $(this).unbind('click');
  return false;
});

#5


0  

If what nickf said doesn't work, you could use a small state machine:

如果说诺夫说不起作用,你可以使用一个小型状态机:

var trigger = 0;

$(document).click(function() { 

  alert('a');
  if(you_want_to_return_false) {
    trigger = 1;
    return false;
  }
});

$(document).click(function() {
  if(trigger !== 0) {
  alert('b');
  } 
  trigger = 0;
});

Not the prettiest solution, but it will work.

不是最漂亮的解决方案,但它会起作用。