0

I have to save temporary data for my webpage using java script.This is the way that i save i one by one since the data is an array.

var product= new Array();
product[1] = document.getElementById("product[1]").value;
product[2] = document.getElementById("product[2]").value;

This method is working. but when i run it by looping, it doesnt work.

for(var i=1; i < no_item; i++){
product[i] = document.getElementById("product[i]").value;
}

*product[] is a varibale that I take from a html dropdown menu

Can anyone please tell me the problem ? thanks ~ =)

4

1 回答 1

1

应该写成,因为每次使用原始代码都会获得 id“product[i]”。这将得到“product[1]”然后是“product[2]”等等:

for(var i=1; i < no_item; i++){
    product.push(document.getElementById("product[" + i + "]").value);
}

另外,作为评论,我们倾向于使用var product = [];javascript var product = new Array();,但两者都可以。

于 2013-11-12T02:23:11.783 回答