【问题标题】:jquery ui autocomplete displays only first item in dbjquery ui自动完成仅显示数据库中的第一项
【发布时间】:2015-08-14 17:21:52
【问题描述】:

我在模板上使用 JQuery 自动完成功能,但是当我得到结果时,自动完成功能只显示一项,尽管获取的结果不止一项。它只显示列表中的第一项!

例子:

如果我有一个带有('type1', 'type2', 'type3')的结果列表

在自动完成中,我输入“t”,它只会在下拉菜单中显示type1

我是jquery的新手,请纠正我的错误(如果有的话)

我的自动完成代码:

$(".fro").each(function() {
        $(this).autocomplete({
                source : function(request, response) {
                    $.ajax({
                    serviceUrl: '${pageContext.request.contextPath}/index.htm',
                    datatype: "json",
                    paramName: "fro",
                    delimiter: ",",
                    data : {
                        term : request.term
                    },      
                    success : function(data) {
                        response($.map(data.result, function(item) {
                            $.each(data, function() {
                                return {

                                       label : this.fro,
                                       value : this.fro
                               }
                            });
                        }));
                    }     
                });
            },
                minLength:1
            });
        });

我的响应控制器如下所示:

@RequestMapping(value = "/getTags.htm", method = RequestMethod.GET, headers="Accept=*/*")
      public @ResponseBody List<SearchFiller> getTags(@RequestParam("fro") String fro) {
          return simulateSearchResult(fro);
      }

      private List<SearchFiller> simulateSearchResult(String fro) {

        List<SearchFiller> data=searchFlightDao.fillerList();

        List<SearchFiller> result = new ArrayList<SearchFiller>();
            for (SearchFiller tag : data) {
                if (tag.getFro().contains(fro)) {
                    result.add(tag);
                }
            }

        return result;
      }

正确答案值得赞赏

【问题讨论】:

    标签: javascript jquery jquery-ui spring-mvc autocomplete


    【解决方案1】:

    让你试试这个:

    $(".fro").each(function() {
        $(this).autocomplete({
            source : function(request, response) {
                $.ajax({
                    serviceUrl: '${pageContext.request.contextPath}/index.htm',
                    datatype: "json",
                    paramName: "fro",
                    delimiter: ",",
                    data : {
                        term : request.term
                    },      
                    success : function(data) {
                        dataArray = new Array();
                        $.each(data, function() {
                            var t = { label : this.fro,  value : this.fro };
                            dataArray.push(t);
                        });
                        response(dataArray);
                    }
                });
            },
            minLength:1
        });
    });
    

    【讨论】:

    • 当每个函数都按照你所说的那样放置时,我收到错误“错误放置的构造”我已经插入了每个......请参阅我的编辑。截至目前,第一个数据本身没有显示
    • 请帮我解决这个问题
    • 对不起老兄..你的编辑不起作用..希望我不应该重新启动我的服务器..这些编辑..我会回来
    • $("data.result").each(data.result, function() { 这也不行
    • 没有错误。在萤火虫 itz 中显示 localhost:8080/xxx/index.htm?term=ty 术语是否正确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多