Skip to content

Instantly share code, notes, and snippets.

@duguying
Created October 29, 2015 06:34
Show Gist options
  • Save duguying/24b87db90e6d18a594b5 to your computer and use it in GitHub Desktop.
Save duguying/24b87db90e6d18a594b5 to your computer and use it in GitHub Desktop.
serialize form field into json object
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
@duguying
Copy link
Author

var data = $("form").serializeObject();
// and then, i'll add some extra param
data["extra"] = "hello world";
$.ajax({
    data: data,
    ....
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment