2011-02-24 43 views
3

是否有可能在这些情况下触发的输入上定义事件。jquery定义自己的事件?

 
a) is called on blur of input IF the remaining conditions are true 
b) if the autocomplete list is visible halt the event until it is closed 
c) if the autocomplete list closes without an item being selected then the event is fired 
d) if the autocomplete list closes with an item being selected the event is not fired 
e) if the reason the blur was caused was because an item in the autocomplete was clicked the event is not fired 

正如你所看到的事件有相当一部分它。 我无法在模糊事件中使用正常的setTimeout,因为用户可能坐在自动完成列表上而没有实际选择任何内容。

也许我可以在自动完成开放设置一个变量,所以我们知道它仍然是开放的。
如果定时器到期并且自动完成仍然打开,我们可以重置定时器。
然后关闭,我们可以取消设置变量。
或选择一个项目,我们可以取消定时器?

您认为如何?

回答

5

有可能使用Trigger函数来触发自己的事件。这意味着您可以在代码中的不同位置调用事件,因此您不必编写复杂的结构。

要获取这样的:$('.yourClass').trigger('myEvent');

链接到这个事件可以通过.bind()来完成,即。 $('.yourClass').bind('myEvent', function() { });

0

您可以使用fling plugin来提高自定义事件并为其添加侦听器。该代码将看起来像:

$(yourinput).blur(function(){ 
    if(....your condition....) 
     $.fling('publish','YourEventName',your data in json); 
} 

要附加一个监听器:

$.fling('subscribe','YourEventName', function(){ 
});