Akshat Akshat - 1 year ago 169
jQuery Question

JQuery serializeArray key value pairs

I'm having a bit of trouble serializing a form

<input type="text" name="name1" value="value1"/>
<input type="text" name="name2" value="value2"/>


Will return

Is it possible to get output in the form


So that they are easier to handle?

Answer Source
var result = { };
$.each(​$('form')​.serializeArray()​, function() {
    result[this.name] = this.value;

// at this stage the result object will look as expected so you could use it
alert('name1 = ' + result.name1 + ', name2 = ' + result.name2);

Live demo.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download