更新javascript全局变量 [英] Updating javascript global variable

查看:67
本文介绍了更新javascript全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net开发工作,我有两个文件,即

i)UserControl

ii).js(javascript)

在.js文件中以下是代码

I am working in asp.net development, I have Two files i.e.
i) UserControl
ii).js (javascript)
In .js File following is code

var varBindNewSalesTranGrid = '';
function BindNewSalesAttribute() {
    $.ajax({
        type: "POST",
        url: "/url of Webmthod returns string",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            varBindNewSalesTranGrid += data.d;
        },
        error: function (xhr, ajaxOptions, thrownError) {
        }
    });
}



在Usercontrol文件中


In Usercontrol file

 $(document).ready(function () {
        BindNewSalesAttribute();
        alert(varBindNewSalesTranGrid);
    });
//Which is file referenced above .js file



当我运行我的usercontrol文件时,我看到varBindNewSalesTranGrid的值为空('').Function返回值'TestString',我在BindNewSalesAttribute函数中赋值varBindNewSalesTranGrid但仍然没有更新值。



如果您有任何想法,请告诉我。


When I run my usercontrol file I see the value of varBindNewSalesTranGrid is empty ('').Function returns value 'TestString' and I am assigning value varBindNewSalesTranGrid in BindNewSalesAttribute function but still value is not getting updated.

if you have any ideas then let me know.

推荐答案

.ajax({
类型:POST,
url:/ weblthod的url返回字符串,
data:'{}',
contentType:application / json; charset = utf-8 ,
dataType:json,
成功:函数(数据){
varBindNewSalesTranGrid + = data.d;
},
错误:函数(xhr,ajaxOptions ,thrownError){
}
});
}
.ajax({ type: "POST", url: "/url of Webmthod returns string", data: '{}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { varBindNewSalesTranGrid += data.d; }, error: function (xhr, ajaxOptions, thrownError) { } }); }



在Usercontrol文件


In Usercontrol file


(document).ready(function(){
BindNewSalesAttribute();
alert(varBindNewSalesTranGrid);
});
//哪个是上面引用的文件.js文件
(document).ready(function () { BindNewSalesAttribute(); alert(varBindNewSalesTranGrid); }); //Which is file referenced above .js file



当我运行我的usercontrol文件时,我看到varBindNewSalesTranGrid的值为空('').Function返回值'TestString',我在BindNewSalesAttribute函数中赋值varBindNewSalesTranGrid但仍然没有更新值。



如果您有任何想法,请告诉我。


When I run my usercontrol file I see the value of varBindNewSalesTranGrid is empty ('').Function returns value 'TestString' and I am assigning value varBindNewSalesTranGrid in BindNewSalesAttribute function but still value is not getting updated.

if you have any ideas then let me know.


在上面的代码中,您将值赋给全局变量在ajax调用成功函数内部。如果您在
In the above code you are assigning the value to the global variable inside a ajax call success function. If you look into the steps of execution in


这篇关于更新javascript全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆