【问题标题】:jQueryUI Autocomplete is not properly filtering results from a JSON filejQueryUI 自动完成没有正确过滤来自 JSON 文件的结果
【发布时间】:2013-05-13 21:00:26
【问题描述】:

我已经在我的测试站点http://karem.kaidez.com/ 上配置了一个带有自动完成小部件的 jQueryUI 驱动的搜索。数据由.json 文件填充,并且所有结果都出现在我希望根据我在搜索框中输入的内容进行过滤时。

搜索框嵌入在 HTML 中,如下所示:

<input type="text" id="searchfield" class="searchfield-style" placeholder="Search..." />

具体的jQueryUI代码如下:

  define("search", ["jquery","jqui"], function($, jqui) { //running via RequireJS
  $("#searchfield").autocomplete({
    source: function( request, response ) {
      $.ajax({
        url: "search.json",
        dataType: "json",
        data: {term: request.term},
        success: function(data) {
          response($.map(data, function(item) {
            return {
              label: item.title
            };
          }));
        }
      });
    },
    minLength: 0,
    select: function(event, ui) {
     //code comes later
    }
  });
});

search.json 看起来像这样:

[

  {
    "title":"lorem",
    "url":"/lorem/"
  },

  {
    "title":"ipsum",
    "url":"/ipsum/"
  },

 false
 /*
  * This 'false' is here because the JSON file is being built via a Jekyll 
  * plugin and adding commas at the end of the final key/value pait.
  * Adding 'false' makes the file valid JSON. 
  */

]

我确实看到了这个其他 SO 问题/答案here,我认为我在确保正确配置 term 参数方面做的一切都是正确的。我想认为修复很简单,但不确定。

【问题讨论】:

    标签: jquery jquery-ui


    【解决方案1】:
    try this
    
        ('#searchfield').autocomplete({
            source: function( request, response ) {
      $.ajax({
        url: "search.json",
        dataType: "json",
        data: {term: request.term},
        success: function(data) {
          response($.map(data, function(item) {
            return {
              label: item.title
            };
          }));
        }
      });
    },
            minLength: 2,
            search: function(oEvent, oUi) {
                var selectedValue = $(oEvent.target).val();
                var aSearch = [];
                $('source').each(function(index, value) {
                    if (value.substr(0, selectedValue .length) == selectedValue ) 
                        aSearch.push(value);               
                });
    
                $(this).autocomplete('option', 'source', aSearch);
            }
        });
    

    【讨论】:

    • 您好 PSR...感谢您的回复!我尝试了您的代码,但不幸的是,我收到了一条控制台错误消息,指出未定义 aCleanData
    • 嗨 PSR ...再次感谢您的代码编辑!可悲的是,我仍然收到相同的控制台错误消息。
    • 嗨 PSR ...再次感谢!错误消息消失了,但遗憾的是,该字段不再填充。我确实使用完整的 jQueryUI 代码、JSON 文件以及输入字段在 HTML 中的表示方式更新了我的原始帖子。测试站点的 URL 也在那里。再次感谢您抽出宝贵时间...非常感谢!
    • 抱歉我找不到问题
    • 看这里stackoverflow.com/questions/7734554/…可能对你有帮助
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2012-06-05
    • 2011-02-20
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2012-08-31
    相关资源
    最近更新 更多