mon*_*tro 5 javascript typeahead bootstrap-typeahead
我正在尝试使用typeahead来显示谷歌的建议.
Ajax调用工作正常,数据正确返回:
在执行返回过程(数据)之前; data包含以"w"开头的字符串数组.
data = ["walmart","weather","well fargo","worldstarhiphop","walgreens","维基百科","白页","世界杯","webmd","天气雷达"]
但是显示的建议显示"未定义"而不是真实的单词.知道我在这里缺少什么吗?谢谢.

<input type="text" class="typeahead" placeholder="Search">
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
source: function (query, process) {
$.getJSON("Home/Suggest", { query: query }, function (data) {
return process(data);
});
}
});
Run Code Online (Sandbox Code Playgroud)
更新:
经过一些研究,我找到了我的问题的答案,如果有人需要,我会在这里发布.
诀窍是 - "进程"回调函数期望结果格式为:
[{value:"string1"},{value:"string2"},{value:"string3"}]
而不只是一个字符串数组.
$('.typeahead').typeahead(
{ hint: true, highlight: true, minLength: 1 }, // options
{
source: function (query, process) { // source dataset, data = array of strings
$.getJSON('Home/Suggest', { query: query }, function (data) {
//data=["string1", "string2", "string3"]
//process callback function needs it
//in a format [{value: "string1"}, {value: "string2"}, {value: "string3"}]
var output = $.map(data, function (string) { return { value: string }; });
process(output);
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3407 次 |
| 最近记录: |