[code="js"]
var Test = Class.create();
Test.prototype =
{
initialize : function()
{
this.txt = "111";
new Ajax.Request("/test/test",{method:"post",parameters:"",onComplete: function(response){alert(this.txt);this.txt = response.responseText;}, asynchronous:false});
},
getTxt : function()
{
return this.txt;
}
}
[/code]
在ajax回调函数中alert(this.txt)就为空,而且赋值也不成功。请问大家怎么解决这样的问题呢?
[code="java"]
this.txt = "111";
scope = this; // <-------------用一个变量指向当前this
new Ajax.Request("/test/test",{
method:"post"
,parameters:""
,onComplete: function(response){
alert(scope.txt); //<-------------- 找到上次保存的this
scope.txt = response.responseText;
}
, asynchronous:false});
[/code]