【问题标题】:global variable is not set in jqueryjquery中没有设置全局变量
【发布时间】:2015-02-15 21:55:10
【问题描述】:
$(function(){
    var bwr_w = null; //global variable

//below function gets the dynamic data 
    function myfunc() { 
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year="+val ,
            cache: false,
            success: function(result){                  
                bwr_w= result.replace(/\s+/g, ''); //want to set the data again

            }
        });
    }

    myfunc(); //my dynamic function gets called

    $(".container_map").mapael({                                    
        map : {
            name : "usa_states"
        },
        plots: {
                bwr_w //this should work as per myfunc()
        }
    });

});

即使我在 ajax 返回中得到某个值,我总是将 bwr_w 值设为 null 我希望将我的 bwr_w 设置为全局变量,这样当我从 ajax 获得一些结果时,它应该更改我的地图引脚。

【问题讨论】:

  • bwr_w 并不是真正的“全球”,window.bwr_w 会。你需要等待返回回调
  • 我试过 window.bwr_w = null;但我仍然得到 null

标签: jquery function variables definition mapael


【解决方案1】:

问题是因为$.ajax 调用是异步的。这意味着您的myfunc 在从AJAX 调用返回数据之前退出。要解决此问题,请将所有依赖于返回数据的代码放在回调中:

$(function () {
    var bwr_w = null; //global variable

    //below function gets the dynamic data 
    function myfunc() {
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year=" + val,
            cache: false,
            success: function (result) {
                bwr_w = result.replace(/\s+/g, ''); //want to set the data again
                $(".container_map").mapael({
                    map: { name: "usa_states" },
                    plots: { bwr }
                });               
            }
        });
    }

    myfunc();
});

如果您希望在每次调用 myfunc 时执行不同的逻辑,请将其作为回调函数传入:

$(function () {
    //below function gets the dynamic data 
    function myfunc(callback) {
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year=" + val,
            cache: false,
            success: function (result) {
                var bwr_w = result.replace(/\s+/g, ''); //want to set the data again
                callback(bwr_w);
            }
        });
    }

    myfunc(function (bwr) {
        $(".container_map").mapael({
            map: { name: "usa_states" },
            plots: { bwr }
        });
    });
});

【讨论】:

  • 谢谢,你能帮我解决一下我的情况。
  • 谢谢,我现在得到了我想要的。非常感谢您的大力帮助。顺便说一句,我使用了回调函数。
【解决方案2】:

看这里

$(function(){
    var bwr_w = null; //global variable

    //below function gets the dynamic data 
    function myfunc(then) { 
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year="+val ,
            cache: false,
            success: function(result){                  
                then && then(result);                   
            }
        });
    }

    myfunc(function() {

      bwr_w= result.replace(/\s+/g, ''); //want to set the data again

    $(".container_map").mapael({                                    
        map : {
            name : "usa_states"
        },
        plots: {
                bwr_w //this should work as per myfunc()
        }
      });
    }); 
});

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2020-07-23
    相关资源
    最近更新 更多