2011-01-31 84 views
0

我有以下腳本,只要#txtAllowSearch是平的HTML工作:自動完成和生活?

​​

爲#txtAllowSearch動態由JavaScript/jQuery的創建這便會立即停止工作。

我是否需要使用jqueries才能使其工作?

+0

或許是肯定的,如果#txtAllowSearch是被動態創建並插入我的DOM – Rafay 2011-01-31 09:33:51

回答

3

jQuerys .live()help // .delegate()help只能望塵莫及事件。在你的情況下(在元素上應用插件方法),你需要在元素插入到DOM後每次調用.autocomplete(),或者使用優秀的插件插件。

2

jQuery.live現已棄用。

爲了達到這個目標,您現在應該使用$(document).on。

$(document).ready(function(){ 
    $(document).on("focus.autocomplete", "#txtAllowSearch", function() { 
     source: "test_array.aspx", 
     delay: 0, 
     select: function (event, ui) { 
      $("#txtAllowSearch").val(ui.item.value); // display the selected text 
      $("#txtAllowSearchID").val(ui.item.id); // save selected id to hidden input 
     } 
    }); 
}); 

欲瞭解更多信息,請參閱jQuery的API文檔:http://api.jquery.com/on/