Google Custom Search API Autocomplete?

前端 未结 2 1088
陌清茗
陌清茗 2021-02-08 18:05

We\'re using the google custom search API (paid-for server-side API) to power our search results.

I\'d like to add an autocomplete feature to the search - however, does

2条回答
  •  长发绾君心
    2021-02-08 18:32

    Got this working something like this - hope this helps someone else :)

    $(function () {
      $('input.search')
        .focus(function () { this.select(); })
        .mouseup(function (e) { e.preventDefault(); })
        .autocomplete({
          position: {
            my: "left top",
            at: "left bottom",
            offset: "0, 5",
            collision: "none"
          },
          source: function (request, response) {
            $.ajax({
              url: "http://clients1.google.com/complete/search?q=" + request.term + "&hl=en&client=partner&source=gcsc&partnerid={GOOGLESEARCHID}&ds=cse&nocache=" + Math.random().toString(),
              dataType: "jsonp",
              success: function (data) {
                response($.map(data[1], function (item) {
                  return {
                    label: item[0],
                    value: item[0]
                  };
                }));
              }
            });
          },
          autoFill: true,
          minChars: 0,
          select: function (event, ui) {
            $(this).closest('input').val(ui.item.value);
            $(this).closest('form').trigger('submit');
          }
        });
    });
    

提交回复
热议问题