javascript - 如何将 Ajax 中的值分配给数组?

标签 javascript php mysql ajax

我的代码应该将“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/

相关文章:

javascript - 没有 iframe 的 Backbone 沙箱

javascript - JQuery cookie 扩展将设置一个带有路径的 cookie 但不会读取它

php - Laravel echo 推送器没有收到广播事件

php - 使用jquery向mysql添加字段

mysql - 在 SQL 中搜索后更新找到的行中的列值

javascript - 滚动到底部的解决方案并不总是到页面的绝对末尾

javascript - 如何在 Polymer 中使用 Javascript 设置 `on-tap` </paper-button> 的行为

php - 跟踪投票并只允许每个成员投一票

javascript - iframe 是否可以重定向父页面?

php - 如何首先获取最近的记录?