0

I have some form:

<form id="AddOrganizationForm">
<select data-val="true" data-val-required="The OrganizationRole field is required." id="Role_0_qq" name="Role[0]">
   <option value="0">Value4</option>
   <option value="1">Value1</option>
   <option value="2">Value2</option>
   <option value="3">Value3</option>
</select>
<select data-val="true" data-val-required="The OrganizationRole field is required." id="Role_0_ww" name="Role[1]">
   <option value="0">Value4</option>
   <option value="1">Value1</option>
   <option value="2">Value2</option>
   <option value="3">Value3</option>
</select>
<input name="some1" value="3" />
<input name="some2" value="5" />
</form>

When I use $("#AddOrganizationForm").serializeObject() I get the the object where Role[0] and Role[1] is just a seppareted objects not an array. How can I fix it?

Maris
  • 4,608
  • 6
  • 39
  • 68
  • in both the select id's are same, intentional ? – Oxi Aug 09 '13 at 04:54
  • Having tried to answer your question I just realised, it was not very precise. Can you give us some indication, *what* exactly you want to find in your array? The selects, all input elements? The options of the selects? Or only the texts/values of the select options and inputs? – Carsten Massmann Aug 09 '13 at 05:06
  • `serializeObject()` is not a jQuery function, see [here](http://stackoverflow.com/questions/8900587/jquery-serializeobject-is-not-a-function-only-in-firefox) – Carsten Massmann Aug 09 '13 at 05:15

2 Answers2

0

use makeArray as in

var objArr=$("#AddOrganizationForm select").makeArray();

and, yes, remove the double id please ... that might cause trouble otherwise

If you want the values of all of the form elements in one array try .serializeArray() instead of .serializeObject(). See under serializeArray() in the jQuery manual for a good example of how to use it.

In your case you get this: JSfiddle

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • Not even close to what I want to get. – Maris Aug 09 '13 at 05:38
  • So, what you want is to logically join the two selects, in the sense that their values should be mapped into the same array? @Nabil Kadimi seems to have understood you correctly, so it is probably just me being a bit slow on the uptake ;-) ... – Carsten Massmann Aug 09 '13 at 05:51
  • I wanted to get one object of form which will have the separeted fields in this object. Because I will send this form using Ajax and the server side should map it to the object in the c#. – Maris Aug 09 '13 at 06:03
  • Thanks for letting me in on this! :-) Then my first sentence was definitely answering "something else" ;-) ... The [JSfiddle](http://jsfiddle.net/ZEWUd/) should nevertheless contain something useful for you ... – Carsten Massmann Aug 09 '13 at 06:10
0

Replace name="Role[1]" &name="Role[2]" with name="Role"

Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
  • So I have to make both `select` with the name='Role'? – Maris Aug 09 '13 at 05:05
  • I believe that won't be a problem, unless you are sending the form to a PHP (or others) page, in which case you should use `name="Role[]"`, and your values can be accessed from JS with `x['Role[]'][0/1]` instead of `x.Role[0/1]`. – Nabil Kadimi Aug 09 '13 at 05:08
  • Yeap. Youre way works. One more question. In my question I have made all simple, but what if I want to complete the array of objects when each have the few fields? What name should I give to the each input? Can you help me please? – Maris Aug 09 '13 at 05:35
  • It will most likely work, and you have nothing to lose to try it – Nabil Kadimi Aug 09 '13 at 05:37
  • What If my `Role` is an object and its content Role.Something=3, Role.More=8, Role.DoIt = 1. ... – Maris Aug 09 '13 at 05:50
  • I get next arrays: Role.DoIt[], Role.Something[]. But I want to get Role[].Something, Role.DoIt[]... – Maris Aug 09 '13 at 05:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35117/discussion-between-maris-and-nabil-kadimi) – Maris Aug 09 '13 at 05:54