简体   繁体   English

在函数jQuery中更改全局变量值

[英]change a global variable value in a function jquery

I have a global variable and I want to change its value in a jQuery function like this: 我有一个全局变量,我想在jQuery函数中更改其值,如下所示:

var namep ='';

$("#page2").bind('pageshow', function (event , data) {
   var perso = $(this).data("url").split("?")[1];;
   perso_name = perso.replace("perso=","");
   namep=perso_name;
   $("#header h2").text(perso_name); 
});

alert(namep);

In my alert I have nothing so I think namep doesn't change. 在我的警报中,我什么也没有,所以我认为namep不会改变。 How can I change its value? 如何更改其价值?

It does get changed; 它确实得到了改变; it's just that you're checking it too soon -- before it is changed. 只是您检查得太早了-在更改之前。 You should put the alert in the handler , where the value is changed: 您应该将警报放在更改了值的handler中:

var namep ='';

$("#page2").bind('pageshow', function (event , data) {
    var perso = $(this).data("url").split("?")[1];;
    perso_name = perso.replace("perso=","");
    namep=perso_name;
    alert(namep);
    $("#header h2").text(perso_name); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM