admin管理员组文章数量:1406048
How to store the javascript variable into array?
I have these variable and I wish to store them into array:
var name=document.forms["form"]["name"].value;
var email=document.forms["form"]["email"].value;
var mobile=document.forms["form"]["mobile"].value;
var q1=document.forms["form"]["q1"].value;
var q2=document.forms["form"]["q2"].value;
var q3=document.forms["form"]["q3"].value;
var l1=document.forms["form"]["logo1"].value;
var l2=document.forms["form"]["logo2"].value;
var l3=document.forms["form"]["logo3"].value;
var p1=document.forms["form"]["photo1"].value;
var p2=document.forms["form"]["photo2"].value;
var p3=document.forms["form"]["photo3"].value;
How to store the javascript variable into array?
I have these variable and I wish to store them into array:
var name=document.forms["form"]["name"].value;
var email=document.forms["form"]["email"].value;
var mobile=document.forms["form"]["mobile"].value;
var q1=document.forms["form"]["q1"].value;
var q2=document.forms["form"]["q2"].value;
var q3=document.forms["form"]["q3"].value;
var l1=document.forms["form"]["logo1"].value;
var l2=document.forms["form"]["logo2"].value;
var l3=document.forms["form"]["logo3"].value;
var p1=document.forms["form"]["photo1"].value;
var p2=document.forms["form"]["photo2"].value;
var p3=document.forms["form"]["photo3"].value;
Share
Improve this question
asked Oct 11, 2013 at 7:30
youaremysunshineyouaremysunshine
3517 gold badges17 silver badges28 bronze badges
3 Answers
Reset to default 1var arr = [];
var name=document.forms["form"]["name"].value;
var email=document.forms["form"]["email"].value;
arr.push(name);
//etc
Using the .push() method
You could also serialize if you are going to post the form.
As simple as
var newArray = [];
newArray[0] = name;
newArray[1] = email;
...
You can try with traditional array:
var myArray = [];
myArray.push(document.forms["form"]["name"].value);
The keys will be numeric (starting from 0).
Or, if you want to preserve string keys, like associative arrays in other languages, you can store your values as an object
var myArray = {};
myArray["name"] = document.forms["form"]["name"].value;
本文标签: Store variable into array javascriptStack Overflow
版权声明:本文标题:Store variable into array javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744966284a2634965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论