2011-02-24 88 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(){ 
});