2011-01-31 115 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/