marioizquierdo / jquery.serializeJSON

Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Checkbox list

John-gecko opened this issue · comments

I have the following within a form

<div class="multiplechoice form-group">
	<label for="checkboxlist1" class="fieldLabel">label text</label>
    <span class="checkboxlist" id="checkboxlist1">
		<input type="checkbox" name="checkboxlist1" id="checkboxlist1_0" value="value 1" /> 
		<label for="checkboxlist1_0">value 1</label><br />


		<input type="checkbox" name="checkboxlist1" id="checkboxlist1_1" value="value 2" /> 
		<label for="checkboxlist1_0">value 1</label><br />


		<input type="checkbox" name="checkboxlist1" id="checkboxlist1_2" value="value 3" /> 
		<label for="checkboxlist1_0">value 1</label><br />
	  
	</span>
</div>

as its a checkbox list the names are all the same but the serializeJSON only stores the last selected value

how would you cope with this situation - a custom function?

Cheers
J

Just name the inputs with an ending []:

<input type="checkbox" name="checkboxlist1[]" value="value 1" />
<input type="checkbox" name="checkboxlist1[]" value="value 2" />
<input type="checkbox" name="checkboxlist1[]" value="value 3" />

That's the way to tell the serializer that the values should be added to the list, not replaced with the last value.

Hi
Yeah I was aware of that one but cannot change the names as the cms relies on the existing names.
I have been able to add the"[]" via js before sending it to serializeJSON

Thanks for all your great work on this
Cheers
J

Oh I see. Yeah this plugin is designed to work with input names.

If you can not change them and you don't need nested values, you may be better off directly using jQuery .serializeArray().