2016-06-05 13 views
0

Ich erstelle ein dynamisches Formular. Welche hat Drag & Drop-Funktion. Wenn der Administrator die Formularerstellung abgeschlossen hat, muss ich die Formulardetails in Db speichern.Erstellen von JSON aus dynamischer Formulargenerierung

Fiddle link für Formularerstellung

Ich weiß nicht, von wo ich anfangen soll oder wie die Form zu durchqueren im JSON-Format zu speichern.

Ich war Json-Format wie dieses

var formDetails = [ 

     { 
      "type":"textbox", 
      "label":"Please enter your name", 
      "id":"txt-1465133974748" 
     }, 
     { 
      "type":"select", 
      "label":"Please Select Option", 
      "id":"select-1865133974748", 
      "fieldOptionDetails":[ 
      { 
       "fieldLabel":"Option 1", 
       "fieldValue":"1" 
      }, 
      { 
       "fieldLabel":"Option 2", 
       "fieldValue":"2" 
      }] 
     }]; 
+0

Gerade als Ausgangspunkt speichern - haben Sie überprüft [html-to-json] (https://www.npmjs.com/package/ html-zu-json)? – OzW

+0

Scheint so, als erfindest du das Rad neu, wenn es bereits verschiedene Formularerstellungs-Skripte gibt, die wahrscheinlich das tun werden, was du brauchst – charlietfl

Antwort

1
$('#btnSaveForm').on('click',function(){ 
var formdetail = []; 
$("ul#form-Container li").each(function(){ 
    var input = {};// crate an object 
    // access each form li elements and add the attributes to input object 
    input.type = "textbox/select"; // depending on your form field type 
    input.label = "form field label"; // similarly add other fields also 
    formdetail.push(input) ; // add object to formdetail array 
}); 
JSON.stringify(formdetail); // use josn.stringify(array) 
});