Javascript 2D数组循环停止

时间:2015-02-08 19:36:31

标签: javascript arrays loops

我正在编写一个程序,我需要从多个输入框中检索值并将它们存储在2D数组中,以便我可以将它们$ .post到php文件。

问题是,循环似乎在达到" scoresScores [j] [k] = $(this).val;"部分。但每当我评论出那部分时,它就会很好地循环。什么似乎是问题?

更新:不仅仅是停止的循环,得分下面的所有代码都没有被执行。

if(flag){

            studIDs = [];
            scoreScores = [[]];
            pScores = [];

            $(".studID"+term).each(function(i){

                studIDs[i] = $(this).text();

                for(var j = 0; j < countTypes; j++){

                    pScores[j] = $("#txtPScoreEdit"+term+"-"+j).val();

                    $(".txtScoreEdit"+term+"-"+j).each(function(k){

                        if(k == i){

                            scoreScores[j][k] = $(this).val();
                        }
                    });
                }
            });

            when($.post("/res/php/editScores.php",{

            studIDs: studIDs,
            scoreSubjEDP: <?php echo $_GET['EDPCode']?>,
            scoreTerm: term,
            scoreScores: scoreScores,
            pScores: pScores

            })).done(function(){

                location.reload();
            });
        }

1 个答案:

答案 0 :(得分:0)

这是因为scoreScores只有一个嵌套数组@ index === 1

因此,当您循环播放j > 0k > 0时,它将会失败。

您只需要确保索引有效。

修改

如果稀疏数组没问题,则以下内容应该有效:

if(k == i){

    if(!scoreScores[j]) { scoreScores[j] = []; }
    scoreScores[j][k] = $(this).val();
}