Created
March 5, 2013 15:51
-
-
Save adardesign/5091258 to your computer and use it in GitHub Desktop.
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
someNameSpace = { | |
//... | |
processForm: function (form) { | |
return JSON.stringify(form.serializeObject(function (key, val) { | |
return { | |
name: key, | |
value: encodeURIComponent(val) | |
}; | |
})); | |
} | |
//... | |
}; |
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
$.fn.serializeObject = function(fn) { | |
var o = {}, | |
a = this.serializeArray(), | |
that; | |
$.each(a, function() { | |
that = this; | |
if($.isFunction(fn)){ | |
that = fn(that.name, that.value); | |
} | |
if(o[this.name] !== undefined) { | |
val = that.value || ''; | |
if(!o[that.name].push) { | |
o[that.name] = [o[that.name]]; | |
} | |
o[that.name].push(that.value); | |
} else { | |
o[that.name] = that.value; | |
} | |
}); | |
return o; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jquery.serializeObject.js is similar to
$.serializeAray
but it turns in the form data into a js object (which is easily converted to JSON by pasing it in toJSON.stringify()
)A callback function is optional to all to manipulate/process the form data..
A typical usage would be to encodeURIComponent..