JQuery 自动完成选择未定义错误
我已经把头发扯了几个小时了,似乎无法找出为什么会出现错误。
当我调用以下代码时:
$(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() 来检查内容,并且我得到这个:
- [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
所以我不明白为什么它是未定义的。
谢谢 :)
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 :
- [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
- [addressLine] =>Quai d'Issy-les-Moulineaux
- [adminDistrict] =>IdF
- [adminDistrict2] =>Paris
- [countryRegion] =>France
- [formattedAddress] =>Quai d'Issy-les-Moulineaux, 75015 Paris
- [locality] =>Paris
- [postalCode] =>75015
So i don't understand why it's undefined.
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文档:
所以我认为你想要
ui.item.long
而不是ui.long
。From the documentation:
So I think you want
ui.item.long
instead ofui.long
.