Javascript 二维数组循环停止
Javascript 2D Array Loop Stopping
我正在编写一个程序,我需要从多个输入框中检索值并将它们存储在一个二维数组中,这样我就可以将它们 $.post 到一个 php 文件中。
问题是,循环似乎在到达 "scoresScores[j][k] = $(this).val;" 部分时停止。但是每当我注释掉那部分时,它就会循环播放。好像是什么问题?
更新:不仅仅是循环停止了,scoresScores 下面的所有代码都没有被执行。
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();
});
}
因为scoreScores
只有一个嵌套数组@index === 1
所以当你循环 j > 0
或 k > 0
时它会失败。
您只需要确保索引有效即可。
编辑:
如果稀疏数组没问题,下面的方法应该有效:
if(k == i){
if(!scoreScores[j]) { scoreScores[j] = []; }
scoreScores[j][k] = $(this).val();
}
我正在编写一个程序,我需要从多个输入框中检索值并将它们存储在一个二维数组中,这样我就可以将它们 $.post 到一个 php 文件中。
问题是,循环似乎在到达 "scoresScores[j][k] = $(this).val;" 部分时停止。但是每当我注释掉那部分时,它就会循环播放。好像是什么问题?
更新:不仅仅是循环停止了,scoresScores 下面的所有代码都没有被执行。
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();
});
}
因为scoreScores
只有一个嵌套数组@index === 1
所以当你循环 j > 0
或 k > 0
时它会失败。
您只需要确保索引有效即可。
编辑:
如果稀疏数组没问题,下面的方法应该有效:
if(k == i){
if(!scoreScores[j]) { scoreScores[j] = []; }
scoreScores[j][k] = $(this).val();
}