Note that there are some explanatory texts on larger screens.

plurals
  1. POFormat JSON on form submit
    primarykey
    data
    text
    <p>(Background) I am building a simple form builder where user will be able to create a survey/questionnaire. Am creating a json output once the user is done designing the form which will be used to create the actual form. Since its a form-builder user, who is designing/building the form, can add checkboxes or radio fields or selectbox options, and can edit their values and labels. HTML:</p> <pre><code>&lt;form class="test-form droppedField checkbox"&gt; &lt;input type="text" name="title" placeholder="Title" class="ctrl-textbox editable"&gt;&lt;/input&gt; &lt;input type="text" name="subtitle" placeholder="Sub Title" class="ctrl-textbox editable"&gt;&lt;/input&gt; &lt;input type="text" name="name" placeholder="Name/Keyword" class="ctrl-textbox editable"&gt; &lt;input type="hidden" name="type" value="checkbox"&gt; &lt;ul&gt; &lt;li&gt; &lt;div class="ctrl-checkboxgroup children" name="children"&gt; &lt;input type="checkbox"&gt; &lt;input name="label" type="text" value="" placeholder="Option Label.." class="input-small editable"&gt; &lt;input type="text" value="" name="value" placeholder="Option Value.." class="input-small editable opt-value"&gt; &lt;/div&gt; &lt;div class="ctrl-checkboxgroup children" name="children"&gt; &lt;input type="checkbox"&gt; &lt;input name="label" type="text" value="" placeholder="Option Label.." class="input-small editable"&gt; &lt;input type="text" value="" name="value" placeholder="Option Value.." class="input-small editable opt-value"&gt; &lt;/div&gt; &lt;/li&gt; &lt;/ul&gt;.... </code></pre> <p>I am stuck at a point where am generating the json. I want to be able to get a cleaner o/p with checkbox options/radio/selectbox options grouped together as 'children'. Sample json (that I think would be helpful):</p> <pre><code>[ { "title": "Gender", "subtitle": "This is a required question.", "name": "gender", "children": [ { "label": "Male", "value": "male" }, { "label": "Female", "value": "female" }, { "label": "Non traditional", "value": "nontraditional" } ] } ] </code></pre> <p>What am getting right now:</p> <pre><code>[ { "title": "Gender", "subtitle": "This is a required question.", "name": "gender", "label":["Male", "Female", "Non traditional"], "value":["male", "female", "nontraditional"] } ] </code></pre> <p>What I am doing:</p> <pre><code>$('.test-form').each(function () { output.push(JSON.stringify($(this).serializeObject())); }); $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; </code></pre> <p>When I push the children to an array iterating over them:</p> <pre><code>obj = []; $('.children').each(function (i,v) { obj.push(obj[v.name] = v.value); }); console.log(obj); </code></pre> <p>I get:</p> <pre><code>["Male", "male", "Female", "female", "Non traditional", "nontraditional", label: "Non traditional", value: "nontraditional"] </code></pre> <p>I am confused if I should re-structure my HTML or if I can alter the JSON output. Any sort of help is appreciated. Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload