【发布时间】:2011-07-19 07:21:02
【问题描述】:
我无法让我的 for 循环读取请求之外的 maxSlots 变量。我不知道如何或在哪里声明变量以使其对每个类都是本地的,但对于孔类是全局的。
//INITIALIZATION
initialize: function(){
maxSlots = 0;
var myRequest = new Request({
url: 'getInventory.php',
method: 'post',
onSuccess: function(responseText){
maxSlots = responseText;
console.log(maxSlots);
},
onSFailure: function(){
alert('noo');
}
});
myRequest.send();
for (var i = 1; i <= maxSlots; i++){
var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
slot.inject(invContainer);
}
}
编辑: 好的,我尝试将变量更改为一个选项,请求中的警报 = 12 但如果我在请求后发出警报,它说未定义......仍然是同样的问题。
//VARIABLES
options: {
id: '',
size: '',
maxSlots: '2'
},
//INITIALIZATION
initialize: function(options){
this.setOptions(options);
var myRequest = new Request({
url: 'getInventory.php',
method: 'post',
onSuccess: function(responseText){
this.maxSlots = responseText;
alert(this.maxSlots)
},
onSFailure: function(){
alert('noo');
}
});
myRequest.send();
alert(this.maxSlots)
for (var i = 1; i <= this.maxSlots; i++){
var slot = new Element('div', {id: 'slot'+i, class: 'slot'});
slot.inject(invContainer);
}
}
【问题讨论】:
标签: javascript ajax mootools request scope