我没有显示typeahead建议我跟着typeahead documentation example: The Basics。当我复制/粘贴来自'The Basics'的示例代码时,它工作正常。我改变的是我做了一个ajax请求来代替静态数组。Typeahead不工作:当我输入
代码:
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
/* only part I really changed */
/* I replaced the static array from the example */
/* with an array I am getting from ajax request. */
var agencies_ary = []
$.ajax({
url: "agencies/get_unique_agency_names",
type: "GET",
success: function(data){
agencies_ary = data;
}
})
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'agencies_ary',
source: substringMatcher(agencies_ary)
});
Ajax请求的工作。它正确地返回一个名称数组。
我试图用上面的建议答案替换下面我的答案中的所有代码。不幸的是,它不适合我。 – Neil