这个调用为什么将一个数组的其他元素存储到一个数组无法正常工作?正如我可以看到,每当我尝试点击SHOW数组后,存储过去的第一个元素,它似乎继续并覆盖arrMenu的所有早期的元素。为什么这个参差不齐的数组没有正确存储?
http://jsfiddle.net/MasterOfKitties/jW5Bv/96/
的操作代码是在这里:
function fnPopArray()
{
arrItem[0] = document.getElementById('idName').value;
arrItem[1] = document.getElementById('idType').value;
arrItem[2] = document.getElementById('idPrice').value;
arrItem[3] = document.getElementById('idCalories').value;
/*We should generate the HTML string here. */
var strHTML = "<b><p>Name:</b>" + arrItem[0]+"</p><p>Type:"+ arrItem[1]+"</p><p>Price:"+arrItem[2]+"</p><p>Calories:"+ arrItem[3]+"</p>";
document.getElementById("idResults").innerHTML = strHTML;
$("#idResults").show();
/*now we generate the ingredients string here */
var strIngHTML = "";
for(var i=0;i<arrItem[4].length;i++)
{
strIngHTML = strIngHTML + arrItem[4][i]+"<br/>";
}
strHTML = "<b>Ingredients</b><br/>"+strIngHTML;
/*We display it in the second div we have for ingredients */
document.getElementById("idIngredients").innerHTML = strHTML;
$("#idIngredients").show();
/*So we have populated and displayed the contents of our item. Now populate it into our menu object.*/
arrMenu[intMenu]=arrItem;
intMenu = intMenu+1;
}
intMenu如何/何时初始化? – Lepidosteus
检查你的控制台。 'arrItem [4]'是未定义的,所以'arrItem [4] .length'会在第65行发出错误。 –
它在代码的最顶层,有很多评论。 arrItem [4]在另一个函数中被调用,所以它不应该是未定义的,虽然它应该能够工作,即使它是空的。 –