我沒有顯示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