I have a form for which I use JQUERY templates, as there can be multiple fields for an attribute. For eg. a user can have multiple phone numbers. So, he can add fields dynamically by pressing the option "add one more number". In this way multiple fields are added but all have the same name.
How should I serialize this kind of form data into a JSON object?
--Thanks in advance
Asked
Active
Viewed 542 times
0

Harshit Agarwal
- 1,345
- 3
- 20
- 27
2 Answers
1
You can serialize a form with $(form).serialize ()
I'm not sure what the element id has to do with anything, but they should be unique on a page.

Jason
- 15,915
- 3
- 48
- 72
-
Thanks. How should I make sure that every time a new template is rendered it has unique id's for each element? Can you provide me with an example. – Harshit Agarwal Jan 13 '12 at 08:03
-
I don't know off the top, maybe when you create the new element from the template – Jason Jan 13 '12 at 09:37
-
You can set an element's id using $(element).attr("id", value); – Jason Jan 13 '12 at 09:45
-
I use something similar to this http://stephenwalther.com/blog/archive/2010/11/30/an-introduction-to-jquery-templates.aspx --as mentioned in the first example – Harshit Agarwal Jan 13 '12 at 10:01
0
You can set name attribute for multiple fields to consider it as array. Example is below
<input type="text" name="phone[]"> <input type="text" name="phone[]"> <input type="text" name="phone[]">
Now for submitting you can use serializeArray() function to post the data to serverside script
I hope this will work for you

Yogesh
- 928
- 1
- 8
- 21
-
But this doesn't work with radio buttons. Instead of creating separate groups they are put all together in a single group. – Harshit Agarwal Jan 13 '12 at 09:44
-
Try to set separate name for radio buttons which are in different group – Yogesh Jan 13 '12 at 11:35