【问题标题】:jquery ui autocomplete undefinedjquery ui 自动完成未定义
【发布时间】:2016-07-02 07:56:01
【问题描述】:
$(function() {
    $("#restaurant_name_search").autocomplete({
        source: function(d, e) {
            $.ajax({
                type: 'GET',
                url: api_url + 'searchrestuarant/' + encodeURIComponent(d.term),
                success: function(b) {
                    var c = [];
                    b = JSON.parse(b);
                    $.each(b, function(i) {
                        i.label = i.Restaurant_Name;
                        c.push(i);
                    });
                    e(c);
                }
            })
        },
        select: function(a, b) {
            console.log(b);.
        }
    }).data("ui-autocomplete")._renderItem = function(ul, item) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + item.label + "</a>") // <---
            .appendTo(ul);
    };
});

这是我的 jquery ui 调用。

json输出如下:

[{
    "Restaurant_Key": "1",
    "Restaurant_Name": "Altitude Espresso",
    "Email": "",
    "Phone_1": "",
    "Local_Restaurant_Key": "1",
    "Address_Line1": "163 Oriordian Street",
    "City": "Mascot"
}]

但自动完成总是显示未定义。

从 url 接收输出。

【问题讨论】:

    标签: javascript jquery jquery-ui autocomplete


    【解决方案1】:

    $.each 的第一个参数是索引,所以你的代码应该是:

     var c = [];
     b = JSON.parse(b);
     $.each(b, function(index, item) {
       item.label = item.Restaurant_Name;
       c.push(item);
     });
    

    但与其手动创建数组并变异原始对象,不如使用$.map

    var c = $.map(b, function(item, index) {
       return {label : item.Restaurant_Name};
    });
    

    在这种情况下,参数的顺序是您所期望的。

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 1970-01-01
      • 2017-05-31
      • 2014-11-22
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多