0

I serialized a form data. In that form, i have checkboxes named "assets".

                    <div class="fot-form">
                        <label class="fot-form__label">Select the assets you own</label><br>
                        <input type="checkbox" name="assets" class="fot-form__check" value="phone">Smartphone<br>
                        <input type="checkbox" name="assets" class="fot-form__check" value="bike">Bike<br>
                        <input type="checkbox" name="assets" class="fot-form__check" value="laptop">Laptop<br>
                        <input type="checkbox" name="assets" class="fot-form__check" value="car">Car<br>
                        <input type="checkbox" name="assets" class="fot-form__check" value="home">Home
                    </div>

In JS after below code var data = $(".test-form").serializeArray(); var testFormJson = .object(.pluck(data, 'name'), _.pluck(data, 'value'));

I got only assets:"car", not an array like assets: ["car", "laptop", "phone"]. How to retrieve this array.

1
  • Why not simply naming it assets[]? It will form array Commented Jun 2, 2016 at 10:26

2 Answers 2

1
<input type="checkbox" name="assets[]">

This will form an array of selected items

Sign up to request clarification or add additional context in comments.

2 Comments

In "testFormJson", i got only this data assets[]:"laptop"
But in "data", i got object{"name":"assets[]", "value:"bike"}, object{"name":"assets[]", "value:"laptop"}, object{"name":"assets[]", "value:"car"}
0

Checkboxes are not radio buttons. A radio button returns a single name/value pair based on a selection and each option has the same name. In a checkbox, each option can not share a name="thing" setting as they all refer to the same thing.

There is no magic here: each checkbox is a single element (similar to a multiple select list). A radio button is way of determining a choice between elements (similar to an input="text").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.