如何 jquery 自动完成 json 结果?

How to jquery autocomplete json results?

我的代码有两个问题:

首先是,json 中的搜索结果仅在我删除文本输入时出现,而不是在我输入时出现……就像我需要的那样。第二个是,当我点击其中一个结果显示它时,我需要文本转到输入文本的值。

我该怎么做?

pastebin 与 the code

pastebin 与 json example

有人能帮帮我吗?

根据自动完成 docs,它不适用于 json 数据,仅适用于数组。

像这样尝试一些想法,应该有效。

var arr = ['project1', 'another project', 'more one project'];

     $("#project").autocomplete({
       minLength: 0,
       source: arr,
       focus: function( event, ui ) {
         $("#project").val( ui.item.label );
         return false;
       }
     });

要在擦除字段时隐藏结果,您应该像这样设置 minLengh <= 1:

$project.autocomplete({
    minLength: 1,
    source: projects,
    focus: function( event, ui ) {
      $project.val( ui.item.label );
      return false;
    }
  });

如果您想在后端搜索数据,我认为您应该考虑使用选项将数据过滤为查询字符串,请查看此文档部分:

String: When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results. For example, if the source option is set to "http://example.com" and the user types foo, a GET request would be made to http://example.com?term=foo. The data itself can be in the same format as the local data described above.