gpt4 book ai didi

javascript - Typeahead 将结果显示为未定义

转载 作者:数据小太阳 更新时间:2023-10-29 06:01:45 26 4
gpt4 key购买 nike

我正在尝试使用 typeahead 来显示谷歌建议。

Ajax 调用工作正常并且数据正确返回:

在执行return process(data);之前数据包含以“w”开头的字符串数组。

data = ["walmart", "weather", "wells fargo", "worldstarhiphop", "walgreens", "wikipedia", "white pages", "world cup", "webmd", "weather radar"]

但是显示的建议显示“未定义”而不是真实的单词。知道我在这里缺少什么吗?谢谢。

enter image description here

    <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);
});
}
});

最佳答案

更新:

经过一些研究,我找到了我的问题的答案,如果有人需要,我会把它贴在这里。

诀窍是——“处理”回调函数需要以下格式的结果:

[{值:“string1”},{值:“string2”},{值:“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);
});
}
});

关于javascript - Typeahead 将结果显示为未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26442052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com