我的代码应该将“seventythird”传递给 php,然后将其发送到 MYSQL 数据库,并根据从表 73 检索的值进行计算以计算效率。之后,它应该再次运行并从 74 中获取值。我为此使用了 for 循环,但结果只是为数组中的位置 0 和 1 发送相同的值。
function calculateEfficiency(){
for (x =0; x < 2; x++){
var y = x+1;
switch(y){
case 1:
mod = "seventythree";
break;
case 2:
mod = "seventyfour";
break;
}
$.ajax({
type: 'post',
url: 'efficiency.php',
dataType: 'json',
data: {'mod' : mod},
error: function(){alert("fail");}
}).done(function(data){
efficiency = data.efficiency;
refresh = data.refresh;
});
efficiencyArray[x] = efficiency;
if (refresh){
document.getElementById("efficiency1").innerHTML = efficiencyArray[0];
document.getElementById("efficiency2").innerHTML = efficiencyArray[1];
}
}
}
我该怎么办?
最佳答案
@barmar 的意思是你需要放置这个:
efficiencyArray[x] = efficiency;
if (refresh){
document.getElementById("efficiency1").innerHTML = efficiencyArray[0];
document.getElementById("efficiency2").innerHTML = efficiencyArray[1];
}
里面:
.done(function(data){
//inside here
});
关于javascript - 如何将 Ajax 中的值分配给数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44419062/