gpt4 book ai didi

javascript - JSON + PHP + JQuery + 自动完成问题

转载 作者:行者123 更新时间:2023-12-01 05:09:53 26 4
gpt4 key购买 nike

今天才开始,但我在尝试理解 JSON/AJAX 等时遇到了很大的问题,到目前为止我已经得到了我的代码,但对如何将 AJAX 请求拉取的数据返回到 jQuery 自动完成功能感到困惑。

var autocomplete = new function() {
this.init = function() {
$('#insurance_destination').autocomplete({
source: lookup
});
}

function lookup() {
$.ajax({
url: "scripts/php/autocomplete.php",
data: {
query: this.term
},
dataType: "json",
cache: false,
success: function(data) {
for (key in data) {
return {
label: key,
value: data[key][0]
}
}
}
});
}
}

PHP 脚本返回的 JSON 字符串示例

{
"Uganda": ["UGA", "UK4", "Worldwide excluding USA, Canada and the Carribbean"]
}

最佳答案

通常情况下,您不必自己进行ajax查询:

$('#insurance_destination').autocomplete('url_here', {options_here});

假设我们正在谈论标准 jquery 自动完成插件。我对你的理解正确吗?

编辑检查api
http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions
还有一些例子。

关于javascript - JSON + PHP + JQuery + 自动完成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2768599/

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