2011-08-03 20 views
1

我正在使用舊版jquery autocomplete plugin,並且如果我的搜索框已被清除,我想觸發一個事件。這是我目前的代碼:如何在空的jquery自動填充字段上調用事件?

jQuery(function($){ 
    $("#searchbox").Watermark("Search"); 
}); 


$("#searchbox").autocomplete("search_names.php", { 
    width: 250, 
    selectFirst: false 
}); 

$("#searchbox").result(function(event, data, formatted) { 
    if (data) { 
     filterView(data); 
    } 
}); 

我試過使用結果觸發器,但它期待一個有效的結果。任何想法如何在搜索框爲空時觸發事件?基本上我想在篩選結果之前恢復搜索結果。

感謝

回答

0

一個可能的解決方案是使用.focus運行while循環被點擊的搜索框中輸入用戶當空場檢查並關閉while循環使用.blur當他們離開搜索框。我沒有嘗試過,所以請讓我知道它是否有效。此致 Kevin

$("#searchbox").focus(function() { 
    var checkNull = true; 

    while (checkNull == true){ 
     if (!data) { 
      if (previous results are not displayed) { 
       your code here to display prior results; 
      } 
     } 
    } 
} 


$("#searchbox").blur(function() { 
    checkNull = false; 
}