【问题标题】:External Json with autocomplete jquery带有自动完成 jquery 的外部 Json
【发布时间】:2014-09-23 14:07:11
【问题描述】:

我尝试使用外部 Json 作为自动完成 Jquery UI 插件的源代码:http://jqueryui.com/autocomplete/

我不明白json的好方法和好格式。在我的示例中,我的 json 中有: ["tag1","tag2","tag3"]

自动完成似乎可以工作,但如果开始输入“a”,自动完成会建议 json 中的所有标签。似乎自动完成不会过滤 json 的内容,并且总是在 json 中显示整个标签。 所以,我不明白如何有正常的行为:例如,当我输入“t”时,自动完成只建议我“tag1”。

我的页面在这里: http://tcdemo.fr/temp/test.html

Json 在这里: http://tcdemo.fr/temp/search.json

非常感谢

【问题讨论】:

    标签: jquery json jquery-ui autocomplete


    【解决方案1】:

    查看您的代码,您可以做三件事来获得解决方案:

    1. 在服务器端处理过滤并在提供过滤结果的端点上调用 ajax 函数
    2. 如果它是一组静态对象,则在 .tagit() 绑定之外加载 json 并将 json 结果传递给 availableTags 属性。默认情况下会处理过滤。

      $("#mytags").tagit({
          availableTags: jsonResult, // search.json is loaded into jsonResult
          show_tag_url: "/tags/",
          singleField: true,
          singleFieldNode: $('#submit_tag_names')
      });
      
    3. 否则,如果出于某种原因更喜欢您的方法 - 在 javascript 端过滤成功结果。将脚本中的成功函数更改为:

      success: function(choices) {
              var filter = search.term.toLowerCase();
              var filteredChoices = $.grep(choices, function(element) {
                  // Only match autocomplete options that begin with the search term.
                  return (element.toLowerCase().indexOf(filter) === 0);
              });
              showChoices(this._subtractArray(filteredChoices, this.assignedTags()));
          }
      

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-16
      • 2010-12-08
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      相关资源
      最近更新 更多