Twitter 引导程序提前类型无法添加到动态创建的元素

Twitter bootstrap typeahead unable to add to dynamically created element

本文关键字:动态 创建 元素 添加 引导程序 类型 Twitter      更新时间:2023-09-26

有没有办法将 Twitter 引导程序的 typeahead 函数转换为 $("body").on() 函数?如果该元素存在于页面加载中,则以下代码工作正常。

$("#job_title").typeahead({
    source: searchFunction,        
    onselect: function(obj) {            
    }
});

但是如果我动态添加带有id="job_title"的文本输入,则上述功能不起作用。 有解决方法吗?

此解决方案不需要额外的 JavaScript。从 https://stackoverflow.com/a/15094730

$("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
    $(this).find(".typeahead").typeahead();
});

我下载了jquery.livequery.js以获得我想要的行为:

$("#job_title").livequery("create", function() {
    $(this).typeahead({
        source: searchFunction,        
        onselect: function(obj) {            
        }
    });
});
var fx = {
    "init":function() {
        $("#id_of_an_element").typeahead({
            minLength:2,
            delay:1 ...
        })
    },
    "method2":function(){
        some content;
    }
}

每次将包含 typeahead 的元素加载到 DOM 中时(例如,通过 AJAX 成功,调用 fx.init();