gpt4 book ai didi

javascript - 如何在jquery函数内的函数内传递变量?

转载 作者:行者123 更新时间:2023-11-28 02:13:55 26 4
gpt4 key购买 nike

所以我正在使用谷歌地图API。我必须在对象内部插入值 lat 和 lon,但我不能执行以下操作:

$.get(url).done(buscaGPS(data, i));

function buscaGPS(data, i) {

}

代码

objecto = [{
"address_1": "Avenida da Republica, 15A",
"city": "Lisbon",
"country": "pt",
"id": 1534282,
"name": "Pastelaria Versailles"
}, {
"address_1": "Avenida da Republica, 15A",
"city": "Lisbon",
"country": "pt",
"id": 1534282,
"name": "Pastelaria Versailles"
}];


for (var i = 0; i < objecto.length; i++) {
var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + objecto[i].address_1 + "+" + objecto[i].city + "+" + objecto[i].country + "&sensor=false";
$.get(url).done(buscaGPS);
};



function buscaGPS(data) {

objecto[i].lat = data.results[0].geometry.location.lat;
objecto[i].lon = data.results[0].geometry.location.lng;

}

最佳答案

听起来你的意思是你想做一些类似的事情......

// create a closure to capture the index
function callback(index){
return function(data) {
objecto[index].lat = data.results[0].geometry.location.lat;
objecto[index].lon = data.results[0].geometry.location.lng;
}
}

for (var i = 0; i < objecto.length; i++) {
var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + objecto[i].address_1 + "+" + objecto[i].city + "+" + objecto[i].country + "&sensor=false";
$.get(url).done(callback(i));
}

关于javascript - 如何在jquery函数内的函数内传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com