【问题标题】:Variable from outside getjson inside it [duplicate]来自外部getjson内部的变量[重复]
【发布时间】:2014-05-17 21:41:26
【问题描述】:

我有以下内容,但标题只是重复相同。我怎样才能在getJSON 函数中使用title

for (var x = 0; x < temp_addresses.length; x++) {
    var title = temp_addresses[x].title;
    var jqxhr = $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+temp_addresses[x].gps+'&sensor=false', null, function(data) {
        var p = data.results[0].geometry.location
        latlng = new google.maps.LatLng(p.lat, p.lng);
        console.log(title+' - '+latlng);
    });
}

【问题讨论】:

标签: javascript jquery ajax json getjson


【解决方案1】:

这是一个关闭问题。

试试这个:

for (var x = 0; x < temp_addresses.length; x++) {
    var title = temp_addresses[x].title;
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?address='+temp_addresses[x].gps+'&sensor=false';
    var jqxhr = performReq(title,url);
}

function performReq(title,url){
    return $.getJSON(url, null, function(data) {
        var p = data.results[0].geometry.location
        latlng = new google.maps.LatLng(p.lat, p.lng);
        console.log(title+' - '+latlng);
    });
}

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2013-02-06
    • 1970-01-01
    • 2021-09-02
    • 2010-09-08
    • 2012-02-22
    相关资源
    最近更新 更多