我正在使用谷歌建议,我正试图得到所有的建议。我在这里找到了例子
<div class="ui-widget">
<input id="search" />
</div>
javascript代码
$(function () {
$("#search").autocomplete({
source: function (request, response) {
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22http%3A%2F%2Fsuggestqueries.google.com%2Fcomplete%2Fsearch%3Fclient%3Dfirefox%26q%3D" + encodeURIComponent(request.term) + "%22&format=json",
dataType: "jsonp",
success: function (data) {
response(data.query.results.json.json[1].json);
}
});
},
minLength: 2
});
});
http://jsfiddle.net/Xotic750/qjy6H/
当我输入多于一个单词时,它不起作用(不显示建议) 感谢帮助。关于这个主题还有另一个问题。我可以将结果保存到数组并将其传输到html页面吗?我怎么能这样做?
答案 0 :(得分:3)
您需要用request.term
符号替换+
中的空格:
以下是正在使用的JS代码:
$(function () {
$("#search").autocomplete({
source: function (request, response) {
var searchTerm = "auto+" + request.term.replace(" ", "+");
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22http%3A%2F%2Fsuggestqueries.google.com%2Fcomplete%2Fsearch%3Fclient%3Dfirefox%26q%3D"
+ encodeURIComponent(searchTerm) + "%22&format=json",
dataType: "jsonp",
success: function (data) {
response(data.query.results.json.json[1].json);
}
});
},
minLength: 2
}); });
您可以看到它在此代码中工作:http://codepen.io/adrenalinedj/pen/MyOZGj