我有一个生成动态子数组的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
来获取数组结构
答案 0 :(得分:3)
使用Textarea中的输出更新了演示:http://jsfiddle.net/zZxaR/13/
注意:添加了几种在控制台中打印数组的方法
免责声明:使用了一些jQuery来简化遍历。不用也可以轻松完成。
<强>代码强>:
function createArray() {
var myArray = [], i = 0, temp;
while(i < 4) myArray[i++] = [];
myArray[0]['apple'] = '';
myArray[1]['mango'] = '';
myArray[2]['grape'] = '';
myArray[3]['pineapple'] = '';
$('input[data-gpname]:checked').each(function() {
var gp = $(this).attr('data-gpname').toLowerCase();
for(i = 0; i < 4; i++) {
if(myArray[i][gp] != undefined) {
myArray[i][gp] = $(this).attr('value');
temp = [];
temp[gp] = $(this).attr('value');
temp['price'] = $('#' + gp + 'qty').val();
myArray[i].push(temp);
}
}
});
console.log(myArray);
prettyPrint(myArray);
}
答案 1 :(得分:3)
此页面的来源为here。
我根据您的评论制作了另一个sample。试试这个,让我知道。
HTML:
<form method="post" id="myform">
Apple
<input type="radio" onclick="constructJSON(this.name)" id="one" name="apple" data-gpname="apple" data-price="10" value="light"/> Light
<input type="radio" onclick="constructJSON(this.name)" id="two" name="apple" data-gpname="apple" data-price="20" value="dark" /> Dark
<input type="radio" onclick="constructJSON(this.name)" id="one1" name="apple1" data-gpname="apple" data-price="120" value="light2"/> Light11
<input type="radio" onclick="constructJSON(this.name)" 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="constructJSON(this.name)" id="three" name="Mango" data-gpname="Mango" data-price="30" value="light"/> Light
<input type="radio" onclick="constructJSON(this.name)" 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="constructJSON(this.name)" id="four" name="Pineapple" data-gpname="Pineapple" data-price="50" value="light"/> Light
<input type="radio" onclick="constructJSON(this.name)" 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="constructJSON(this.name)" id="six" name="Grape" data-gpname="Grape" data-price="70" value="light"/> Light
<input type="radio" onclick="constructJSON(this.name)" 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>
脚本:
var rgArray = [];
function constructJSON(name)
{
rgArray.push(name);
var myArray = {};
var productData = {};
productData['products'] = createArray();
$("#a").html(JSON.stringify(productData));
}
function createArray(){
var group = {};
var product;
var data;
var hasValue = false;
$rgArray = $(rgArray);
$.each($rgArray, function(i, gp){
product = {};
var el = "input[data-gpname='"+gp+"']";
$el = $(el);
hasValue = false;
$.each($el, function(i, item){
if(item.checked){
data = {};
data.name = $(item).val();
data.price = $(item).attr("data-price");
hasValue = true;
product[$(item).attr("name")] = data;
}
});
if(hasValue) group[gp] = product;
});
return group;
}
答案 2 :(得分:1)
贝娄只是参考我们:
在jQuery中,serialize array函数用于以数组格式打印/显示值。
$('form').submit(function() {
console.log($(this).serializeArray());
return false;
});
答案 3 :(得分:-1)
试试这段代码我刚刚编辑了一下
function createSubArray(name){
var arr = [];
elems = document.getElementsByName(name);
for (var i=0;i<elems.length;i++){
if (elems[i].checked){
var o = {}
o[name] = elems[i].value;
o['price'] = elems[i].getAttribute('data-price');
arr.push(o);
}
}
return arr;
}
function createArray() {
var myArr = [];
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;
}
之前你从createSubArray
方法返回了一个对象,我认为这是一个错误
我已编辑它以返回包含所有对象的数组
这是我得到的JSON字符串
[
[
{
"apple": "light",
"price": "10"
},
{
"apple": "dark",
"price": "20"
}
],
[
{
"Mango": "light",
"price": "30"
},
{
"Mango": "dark",
"price": "40"
}
],
[
{
"Pineapple": "light",
"price": "50"
},
{
"Pineapple": "dark",
"price": "60"
}
],
[
{
"Grape": "light",
"price": "70"
},
{
"Grape": "dark",
"price": "80"
}
]
]