Skip to content Skip to sidebar Skip to footer

Form Key+value Pairs Jquery Plugin

Is there a way I can access the array of data that the browser compiles on a form submit - before the actual GET/POST operation in js/jq? $('form').submit(function(event){ event.p

Solution 1:

Yes, jQuery has a "serializeArray()" method that does pretty much what you ask for.

It returns an array like:

[ { name: "something", value: "whatever" }, { name: "another_one", value: 22 } ]

You can turn that into an object like this:

var arr = $('#form_id').serializeArray(), obj = {};
for (var i = 0; i < arr.length; ++i)
  obj[arr[i].name] = arr[i].value;

If a name appears more than once in a form, then its "value" will be an array of values from the separate fields.

Solution 2:

$data = $("form").serialize(); // will return query string

$data = $("form").serializeArray(); // will return array 

(Details here)

Solution 3:

You can use the $('#form').serializeArray() method in order to get the array passed to the POST request.

You can then edit the object you receive normally.

Post a Comment for "Form Key+value Pairs Jquery Plugin"