问题是.:在下面的代码中......当我调用方法“addtocart”时......当数组只有一个元素时它会正确打印......但如果它有超过1个元素,它会给出错误,如图所示在图片中:(忽略第一个“404”错误)
var counter=0;
function addToCart(productName,productImageURL,productPrice)
{
alert("in addTOCART() method");
var cartArray=new Array();
cartArray[counter]=doTask(productName, productImageURL, productPrice);
//cartArray.concat(doTask(productName, productImageURL, productPrice));
/*cartArray.push(doTask(productName, productImageURL, productPrice));*/
alert("came back to original method");
alert(cartArray.length);
for ( var i = 0; i < cartArray.length; i++) {
var array_element = cartArray[i];
console.log(array_element.toString());
}
alert(cartArray.toString());
counter++;
}
function doTask(productName,productImageURL,productPrice)
{
alert("inside java script");
var cartItem = new Array();
itemName=productName;
itemPrice=productPrice;
alert(itemPrice);
itemImageURL=productImageURL;
quantity=prompt("Please enter the quantity","1");
quantity=parseInt(quantity);
alert(quantity+2);
if (!isNaN( quantity ))
{
alert("yes its a number");
alert(itemImageURL);
alert(itemPrice);
alert(quantity);
alert(itemName);
alert(itemImageURL);
totalAmount=itemPrice*quantity;
alert(totalAmount);
cartItem.push(itemImageURL,itemName,itemPrice,quantity,totalAmount);
//alert(cartItem.toString());
console.log(cartItem.toString());
return cartItem;
}