小编alk*_*333的帖子

jQuery.ajax()中的范围问题

我通过将标签字符串保存到变量中来缓存标签字符串,但遇到了奇怪的范围问题.我知道这与闭包有关,但我似乎无法弄清楚究竟是什么问题.

info_lbl = {};

$("#chkCorporateGift").click(function(){
    var type = $(this).is(":checked") ? "Corporate" : "Personal";
    if(!info_lbl.hasOwnProperty(type)){
        $.ajax({
            url: svc_og + "Get" + type + "InformationLabel",
            success: function(data){
                info_lbl[type] = data;
            }
        });
    }
    $("#lblInformationType").text(info_lbl[type]);
});
Run Code Online (Sandbox Code Playgroud)

lblInformationType标签未在第一次调用GetCorporateInformationLabel或GetPersonalInformationLabel方法时设置.在第一次调用每个人之后,标签的值正在改变.有人可以解释为什么会出现这种情况吗?当我使用Firebug并设置断点时$("#lblInformationType").text(info_lbl[type]);,info_lbl[type]包含正确的值,并且前两个调用的一切正常.

javascript ajax jquery closures

4
推荐指数
1
解决办法
106
查看次数

标签 统计

ajax ×1

closures ×1

javascript ×1

jquery ×1