我有一个生成动态子数组的 javascript 代码。
function createArray() {
var myArr = new Object();
myArr[0] = createSubArray('apple');
myArr[1] = createSubArray('Mango');
myArr[2] = createSubArray('Pineapple');
myArr[3] = createSubArray('Grape');
document.getElementById("a").innerHTML = JSON.stringify(myArr);
return myArr;
}
function createSubArray(name){
var arr = new Object();
elems = document.getElementsByName(name);
for (var i=0;i<elems.length;i++){
if (elems[i].checked){
arr[name] = elems[i].value;
arr['price'] = elems[i].getAttribute('data-price');
}
}
return arr;
}
我得到如下输出
array
0 =>
array
'apple' => string 'light' (length=5)
'price' => string '10' (length=2)
1 =>
array
'apple' => string 'light1' (length=5)
'price' => string '10' (length=2)
2 =>
array
'Mango' => string 'dark' (length=4)
'price' => string '40' (length=2)
3 =>
array
'Pineapple' => string 'dark' (length=4)
'price' => string '60' (length=2)
4 =>
array
'Grape' => string 'dark' (length=4)
'price' => string '80' (length=2)
但我需要排列如下
array
0 =>
array
'apple' => string 'light' (length=5)
0 =>
array
'apple' => string 'light' (length=5)
'price' => string '10' (length=2)
1 =>
array
'apple' => string 'light1' (length=5)
'price' => string '10' (length=2)
1 =>
array
'Mango' => string 'light' (length=5)
0 =>
array
'Mango' => string 'light' (length=5)
'price' => string '10' (length=2)
2 =>
array
'Pineapple' => string 'light' (length=5)
0 =>
array
'Pineapple' => string 'light' (length=5)
'price' => string '10' (length=2)
所以我改变了这样的javascript
function createArray()
{
var myArr = new Object();
var _tempa = new Array();
var elems = document.getElementsByTagName("input");
for (var i=0;i<elems.length;i++)
{ if (elems[i].checked){
_tempa.push(elems[i].getAttribute('name'));
myArr[i] = createSubArray1(_tempa);
}
}
document.getElementById("a").innerHTML = JSON.stringify(myArr);
return myArr;
}
function createSubArray1(namearr){
var arr1 = new Object();
for (var j=0;i<namearr.length;j++){
var elems = document.getElementsByName(name[j]);
for (var i=0;i<elems.length;i++){
if (elems[i].checked)
{
arr1[elems[i].getAttribute('data-gpname')] = createSubArray(elems[i].getAttribute('name'));
}
}
}
return arr1;
}
function createSubArray(name){
var arr = new Object();
var elems = document.getElementsByName(name);
for (var i=0;i<elems.length;i++){
if (elems[i].checked)
{
arr['productname'] = elems[i].value;
arr['price'] = elems[i].getAttribute('data-price');
}
}
return arr;
}
在此之后,我得到如下输出
[{},{}]
我的 HTML 在下面
<form method="post">
Apple
<input type="radio" onclick="createArray()" id="one" name="apple" data-gpname="apple" data-price="10" value="light"/> Light
<input type="radio" onclick="createArray()" id="two" name="apple" data-gpname="apple" data-price="20" value="dark" /> Dark
<input type="radio" onclick="createArray()" id="one1" name="apple1" data-gpname="apple" data-price="120" value="light2"/> Light11
<input type="radio" onclick="createArray()" id="two1" name="apple1" data-gpname="apple" data-price="210" value="dark1" /> Dark22
<input type="text" id="appleqty" name="appleqty" value="" />
<br>
Mango
<input type="radio" onclick="createArray()" id="three" name="Mango" data-gpname="Mango" data-price="30" value="light"/> Light
<input type="radio" onclick="createArray()" id="one" name="Mango" data-gpname="Mango" data-price="40" value="dark" /> Dark
<input type="text" id="Mangoqty" name="Mangoqty" value="" />
<br>
Pine Apple
<input type="radio" onclick="createArray()" id="four" name="Pineapple" data-gpname="Pineapple" data-price="50" value="light"/> Light
<input type="radio" onclick="createArray()" id="five" name="Pineapple" data-gpname="Pineapple" data-price="60" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />
<br>
Grape
<input type="radio" onclick="createArray()" id="six" name="Grape" data-gpname="Grape" data-price="70" value="light"/> Light
<input type="radio" onclick="createArray()" id="seven" name="Grape" data-gpname="Grape" data-price="80" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />
<textarea name="a" id="a" cols="50"></textarea>
<input type="submit" name="se" value="se" />
</form>
我已经习惯于php json_decode
获取数组结构