2017-03-18 62 views
-2

我想创建产生下面的HTML形式的JSON:生成以下表单的对象/数组格式是什么?

<div class="row"> 
     <div class="column row-inner"> 
     <label>First name</label> 
     <input type="text" value=""> 
     </div> 
     <div class="column row-inner"> 
     <label>Last name</label> 
     <input type="text" value=""> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="column row-inner"> 
     <label >Message</label> 
     <input type="text" value=""> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="column column-big row-inner"> 
     <label>Message</label> 
     <input type="text" value=""> 
     </div> 
     <div class="column row-inner"> 
     <label>Message</label> 
     <input type="text" value=""> 
     </div> 
     <div class="column row-inner"> 
     <label>Message</label> 
     <input type="text" value=""> 
     </div> 
    </div> 

我想创建一个数组,并且具有内多个阵列:

schema: [{ // row 
    [{ // row-inner 
     name: 'First name', // label 
     type: 'text', // input 
    }, { 
     name: 'Last name', 
     type: 'text' 
    }] 
    }] 

但是,我发现它过于复杂(I我已经困惑了自己)。

有没有人有更好的建议?

+0

成全它的对象数组的数组! '[[{name:...,type:...},{name:...,type:...}],[...],[...],...]' –

+0

这是一个无效的结构'[{[{....}]}]'如果'schema'应该包含多个表单''''''''var schema = [[{name:...,type: ...},...],[{name:..,type:...},...],...]' – Titus

回答

1
// the form array 
[ 
    // the first row 
    [ 
     // the first column 
     { 
      // the label 
      name: "First name", 
      // the input 
      type: "text" 
     }, 
     // the second column 
     { 
      name: "Last name", 
      type: "text" 
     } 
    ], 
    // the second row 
    [ 
     { 
      name: "Message", 
      type: "text" 
     } 
    ], 
    // the third row 
    [ 
     { 
      name: "Message", 
      type: "text" 
     }, 
     { 
      name: "Message", 
      type: "text" 
     }, 
     { 
      name: "Message", 
      type: "text" 
     } 
    ] 
] 

形式将是一个这样的数组:

form = [row, row, row, row, ...] 

其中是一个这样的数组:

row = [column, column, column, ...] 

是一个对象在这种格式:

column = { 
    name: "label's text", 
    type: "input's type" 
} 

jQuery代码变换上述结构成一种形式:

var form = ...; 

var $form = $("<form></form>"); 
form.forEach(function(row) { 
    var $row = $("<div></div>") 
     .addClass("row") 
     .appendTo($form); 

    row.forEach(function(column) { 
     var $column = $("<div></div>") 
      .addClass("column row-inner") 
      .appendTo($row); 

     $("<label></label>").text(column.name).appendTo($column); 
     $("<input/>").attr("type", column.type).appendTo($column); 
    }); 
}); 

// append $form to a container 
1

如何人对象数组?

var people = []; 

function Person(firstName, lastName) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
} 

var person = new Person('Foo', 'Bar'); 
people.push(person); 

console.log(people[0]); 
Person {firstName: "Foo", lastName: "Bar"} 

小提琴:https://jsfiddle.net/q1a7k30L/

相关问题