Last active
August 29, 2015 14:00
-
-
Save nmeylan/11502628 to your computer and use it in GitHub Desktop.
jQuery serialize form in JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.endsWith = function(suffix) { | |
return this.indexOf(suffix, this.length - suffix.length) !== -1; | |
}; | |
//JSON | |
$.fn.serializeJSON = function () { | |
var json = {}; | |
jQuery.map($(this).serializeArray(), function (n, i) { | |
if(n['name'].endsWith('[]')){ | |
if(json[n['name']] === undefined) | |
json[n['name']] = []; | |
json[n['name']].push(n['value']); | |
} | |
else | |
json[n['name']] = n['value']; | |
}); | |
return json; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment