Convert form data to JavaScript object with jQuery
serializeArray
already does exactly that. You just need to message the data into your required format:
Method 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$.fn.serializeObject = function(){ var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); } |
Method 2:
1 2 |
var data = {}; $("form").serializeArray().map(function(x){data[x.name] = x.value;}); |