自动完成和直播?

时间:2011-01-31 09:29:40

标签: javascript jquery-ui jquery jquery-ui-autocomplete

我有以下脚本,只要#txtAllowSearch是平面html:

$(document).ready(function(){
    $("#txtAllowSearch").autocomplete({
        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
        }
    });
});

一旦#txtAllowSearch由javascript / jquery动态创建,这就会停止工作。

我是否需要使用jqueries来实现这一点?

2 个答案:

答案 0 :(得分:3)

jQuerys .live() help // .delegate() help 仅捕获事件 。在您的情况下(对元素应用插件方法),您需要在元素插入DOM后每次调用.autocomplete(),或使用优秀的.livequery {{3} } 插件。

答案 1 :(得分:2)

jQuery.live现已弃用。

要实现这一点,你现在应该是$(文件).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/

相关问题