2012-02-13 50 views
3

此事件被连接到一组元素:递归jQuery的事件

var elems = $(this).filter(function(){ 
    ... 
}); 

我怎样才能触发更改事件处理程序中这些元素的变化事件,却忽略了当前元素?

elems.bind('change input', function(){ 

    // do some stuff... 

    // then trigger change, but not on "this", to avoid the recursion issue 
    elems.change(); 

}); 

基本上我想OT忽略elemsthis,但只是在我的 “手册” 的变化() - 拨打以上......

回答

3
var elems = $(this).filter(function(){ 
    ... 
}); 

elems.bind('change input', function(){ 


    // then trigger change, but not on "this", to avoid the recursion issue 
    elems.not(this).change(); 

}); 

虽然我必须说,这种情况看起来像一个糟糕的设计问题。
当元素得到改变它设置一个变化事件回调到一些元素,包括他自己......听起来别扭和递归仅仅是一个症状到其他更大的问题......

它可能在AA完成更清洁的方式然后这个。

+0

+1 Re。糟糕的设计问题 – 2012-02-13 16:11:51

+1

@RoryMcCrossan。听起来很尴尬,递归只是一个症状... – gdoron 2012-02-13 16:14:01

3

试试这个:

elems.not(this).change(); 
+0

但我想保留的所有元素的事件,我只是不希望被列入当我触发手动更改 – Alex 2012-02-13 16:07:40

+0

@Alex啊我明白,我已经更新了我的答案。 – 2012-02-13 16:08:47

2

你可以只筛选出当前元素:

elems.not(this).trigger('change'); // or just .change()