1

我有一个生成动态子数组的 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获取数组结构

4

4 回答 4

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);
}
于 2012-09-21T08:48:53.827 回答
3

希望会对你有所帮助。输出与您在此处所说的相同。

这个页面的来源在这里

我根据您的评论制作了另一个样本。试试这个,让我知道。

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;
}
于 2012-09-25T08:44:39.130 回答
1

对于多子数组,我在这里创建了一个示例和代码

贝娄仅供参考:

在 jQuery 表单中,序列化数组函数用于以数组格式打印/显示值。

$('form').submit(function() {
  console.log($(this).serializeArray());
  return false;
});
于 2012-09-25T06:38:21.950 回答
-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"
        }
    ]
]
于 2012-09-21T07:08:47.297 回答