问题描述
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;}}在ajax回调函数中alert(this.txt)就为空,而且赋值也不成功。请问大家怎么解决这样的问题呢?
解决方案
this.txt = "111";scope = this; // <-------------用一个变量指向当前thisnew Ajax.Request("/test/test",{method:"post",parameters:"",onComplete: function(response){ alert(scope.txt); //<-------------- 找到上次保存的this scope.txt = response.responseText;}, asynchronous:false});
时间: 2024-12-22 02:05:06