简体   繁体   English

如何更改全局变量的值

[英]How can I change value of Global variable

Default variable set var a = ''; var b = ''; var html =' <td>'+a+b+'</td>' 默认变量set var a = ''; var b = ''; var html =' <td>'+a+b+'</td>' var a = ''; var b = ''; var html =' <td>'+a+b+'</td>' var a = ''; var b = ''; var html =' <td>'+a+b+'</td>' ; var a = ''; var b = ''; var html =' <td>'+a+b+'</td>' ;

then I create another event use ajax 然后我创建另一个事件使用ajax

$('element').click(function(){
....

         //when ajax was success  change value of variable 
         a = 'webb';
         b = 'sam';
         // And append the variable "html"
         $(div).append(html );  

but the variable a & b is empty. 但是变量a和b为空。 why?? 为什么??

I think the problem is that you have assigned "html" variable value of "<td> + a + b + </td>" when a & b were empty and then never changed it. 我认为问题在于,当a和b为空但从未更改时,您已为“ <td> + a + b + </ td>”分配了“ html”变量值。 Try: 尝试:

     a = 'webb';
     b = 'sam';
     // And append the variable "html"
     html =' <td>'+a+b+'</td>'
     $(div).append(html); 

Just to give some clarity: this is not related with variables being global or local. 只是为了清楚起见:这与全局变量或局部变量无关。 You get empty " <td></td> " because variable "html" is assigned a value (ie it get's normal string value rather than a reference to a and b variables). 您会得到空的“ <td></td> ”,因为为变量“ html”分配了一个 (即,它获得的是常规字符串值,而不是对a和b变量的引用)。

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

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