有没有办法将 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();