twitter bootstrap typeahead无法添加到动态创建的元素

有没有办法将twitter bootstrap的typeahead函数转换为$(“body”)。on()函数? 如果页面加载中存在该元素,则以下代码可以正常工作。

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

但是如果我动态地添加id =“job_title”的文本输入,则上述function不起作用。 这有解决方法吗?

此解决方案不需要额外的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) { } }); });