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

时间:2011-09-14 17:12:14

标签: javascript jquery undefined jquery-autocomplete

我现在已经把头发撕掉几个小时了,似乎无法找出我错误的原因。

当我拨打以下代码时:

 $(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] => object
    • [data] => object
      • [__ type] =>地点:http://schemas.microsoft.com/search/local/ws/rest/v1
      • [bbox] =>对象
        • [0] =&gt ; 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
  • [地址] =>对象
    • [addressLine] => Quai d'Issy-les -Moulineaux
    • [adminDistrict] => IdF
    • [adminDistrict2] =>巴黎
    • [countryRegion] =>法国
    • [格式化地址] => Quai d'Issy-les-Moulineaux,75015 Paris
    • [locality] => Paris
    • [postalCode] => 75015
  • [置信度] =>媒体
  • [entityType] => RoadBlock
  • [label] => Quai d'Issy-les-Moulinea ux,75015 Paris [48.83618 2.26764](法国)
  • [value] => Quai d'Issy-les-Moulineaux,75015 Paris
  • [long] =&gt; 48.83618 < / li>
  • [lat] =&gt; 2.26764
  • 所以我不明白为什么它没有定义。

    谢谢:)

    1 个答案:

    答案 0 :(得分:0)

    来自documentation

    ui.item refers to the selected item.
    

    所以我认为您需要ui.item.long而不是ui.long