JQuery自动完成选择未定义错误

JQuery Autocomplete select undefined error

本文关键字:选择 未定义 错误 JQuery      更新时间:2023-09-26

几个小时以来我一直在绞尽脑汁,似乎不知道为什么出错了。

当我调用以下代码时:

 $(document).ready(function () {
        $("#searchBox").autocomplete({
            select: function (event, ui) {
                $("#searchBox").attr("readonly", true);
                //this is where if i call alert(ui.long) I get undefiend
                $("#CoorLong").val(ui.long);
                $("CoorLat").val(ui.lat);
                print_r(ui);
            },
            source: function (request, response) {
                $.ajax({
                    url: "http://dev.virtualearth.net/REST/v1/Locations",
                    dataType: "jsonp",
                    data: {
                        key: "bingKey",
                        q: request.term
                    },
                    jsonp: "jsonp",
                    success: function (data) {
                        var result = data.resourceSets[0];
                        if (result) {
                            if (result.estimatedTotal > 0) {
                                response($.map(result.resources, function (item) {
                                    return {
                                        data: item,
                                        label: item.name + '[' + item.point.coordinates[0] + ' ' + item.point.coordinates[1] + ']' + ' (' + item.address.countryRegion + ')',
                                        value: item.name,
                                        long: item.point.coordinates[0],
                                        lat: item.point.coordinates[1]
                                    }
                                }));
                            }
                        }
                    }
                });
            },
            minLength: 1
        });
    });

正如我在selec: function(event, ui)中所说的,当我调用ui时。项或ui。值或ui。我总是得到undefined

我实现了print_r()来检查内容,我确实得到了这个:

  • [项目]=祝辞对象
    • (数据)=祝辞对象
      • [__type] =祝辞;地点:http://schemas.microsoft.com/search/local/ws/rest/v1
      • [bbox] =祝辞对象
        • [0] =祝辞48.83231728242932
        • [1] =祝辞2.2598159619433122
        • [2] =祝辞48.840042717570675
        • [3] =祝辞2.275464038056688
    • [名字]=祝辞多尔'Issy-les-Moulineaux,75015巴黎
    • [point] =>object
    • [type] => point
    • [0] =>object
    • [1] =>2.26764
  • [addressLine] =>object
  • [adminDistrict] =>IdF
  • [adminDistrict] =>巴黎
  • [countryRegion] =>法国
  • [formattedAddress] =>巴黎
  • [countryRegion] =>75015年巴黎
  • [地点]=在巴黎
  • [postalCode] =祝辞75015
  • (信心)=祝辞中
  • [entityType] =祝辞路障
  • [标记]=祝辞多尔'Issy-les-Moulineaux, 75015年巴黎[48.83618 - 2.26764](法国)
  • (价值)=祝辞多尔'Issy-les-Moulineaux, 75015年巴黎
  • [时间]=祝辞48.83618
  • (lat) =祝辞2.26764
  • 所以我不明白为什么它是undefined

    谢谢你

    来自文档:

    ui.item refers to the selected item.
    

    所以我认为你想要ui.item.long而不是ui.long