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

发布于 2024-12-04 14:12:20 字数 3064 浏览 1 评论 0原文

我已经把头发扯了几个小时了,似乎无法找出为什么会出现错误。

当我调用以下代码时:

 $(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.item 或 ui.value 或 ui.long 时,我总是得到未定义的结果

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

  • [item] => 对象
    • [data] => 对象
      • [__type] => 位置:http://schemas.microsoft.com/search/local/ws/rest/v1
      • [bbox] => 对象
        • [0 ] =>48.83231728242932
        • [1] =>2.2598159619433122
        • [2] =>48.840042717570675
        • [3] =>2.275464038056688
    • [名称] =>Quai d'Issy-les-Moulineaux, 75015 Paris
    • [点] =>对象
      • [类型] =>点
      • [坐标] =>对象
        • [ 0] =>48.83618
        • [1] =>2.26764
  • [地址] =>对象
    • [addressLine] =>Quai d'Issy-les-Moulineaux
    • [adminDistrict] =>IdF
    • [adminDistrict2] =>巴黎
    • [countryRegion] =>法国
    • [ formattedAddress] =>Quai d'Issy-les-Moulineaux, 75015 巴黎
    • [地点] =>巴黎
    • [邮政编码] =>75015
  • [信心] =>中等
  • [实体类型] =>RoadBlock
  • [标签] =>Quai d'Issy-les-Moulineaux, 75015 巴黎[48.83618 2.26764] (法国)
  • [值] =>Quai d'Issy-les-Moulineaux, 75015 Paris
  • [long] =>48.83618
  • [lat] =>2.26764
  • 所以我不明白为什么它是未定义的。

    谢谢 :)

    I've been ripping my hair out for a few hours now and can't seem to find out why i've got an error.

    When i call the following code :

     $(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
            });
        });
    

    As I said in the selec: function(event, ui) when i call ui.item or ui.value or ui.long I always get undefined

    I implemented a print_r() to check the content and I do get this :

  • [item] =>object
    • [data] =>object
      • [__type] =>Location:http://schemas.microsoft.com/search/local/ws/rest/v1
      • [bbox] =>object
        • [0] =>48.83231728242932
        • [1] =>2.2598159619433122
        • [2] =>48.840042717570675
        • [3] =>2.275464038056688
    • [name] =>Quai d'Issy-les-Moulineaux, 75015 Paris
    • [point] =>object
      • [type] =>Point
      • [coordinates] =>object
        • [0] =>48.83618
        • [1] =>2.26764
  • [address] =>object
    • [addressLine] =>Quai d'Issy-les-Moulineaux
    • [adminDistrict] =>IdF
    • [adminDistrict2] =>Paris
    • [countryRegion] =>France
    • [formattedAddress] =>Quai d'Issy-les-Moulineaux, 75015 Paris
    • [locality] =>Paris
    • [postalCode] =>75015
  • [confidence] =>Medium
  • [entityType] =>RoadBlock
  • [label] =>Quai d'Issy-les-Moulineaux, 75015 Paris[48.83618 2.26764] (France)
  • [value] =>Quai d'Issy-les-Moulineaux, 75015 Paris
  • [long] =>48.83618
  • [lat] =>2.26764
  • So i don't understand why it's undefined.

    Thank you :)

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    未蓝澄海的烟 2024-12-11 14:12:20

    文档

    ui.item refers to the selected item.
    

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

    From the documentation:

    ui.item refers to the selected item.
    

    So I think you want ui.item.long instead of ui.long.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文