0

I have a text field: <input type="text" class="field blink" name="j-state[]" id="j-state"/>

The data in it will be a comma-separated list of states, e.g. New York,New Jersey.

I am using serialize() to grab the form's data. I want to form an array of these states, and pass the array along with serialized data.

Can any help me to achieve this?

Thanks in advance.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Akki
  • 1,718
  • 2
  • 29
  • 53

1 Answers1

1

I did not want to have to deal with your name with the dash so I renamed it thus:(you can deal with that issue yourself otherwise if you like)

<form id="myform">
<input type="text" class="field blink" name="jstate[]" id="jstate"/>
</form>

Get some code from this questions answer: Convert form data to JavaScript object with jQuery

then do this:

$('#jstate').val('New Yourk, Fridaay Town,Frenchville');
var jd = $('#myform').serializeObject();
var ms = jd.jstate[0].split(",");
alert(ms[1]);// alerts " Friday Town"

You can then use toJSON() (google that) to properly form that ms object and send it off - I will leave that exercise to you.

And, last off, a fiddle for you to play with: http://jsfiddle.net/MHVeC/

Community
  • 1
  • 1
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100