0

我正在使用 google-suggestion,我正在尝试获得所有建议。我在这里找到了示例

<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 页面吗?我该怎么做?

4

1 回答 1

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

你可以看到它在这个 codepen 上工作:http ://codepen.io/adrenalinedj/pen/MyOZGj

于 2016-04-06T08:39:27.033 回答