本文主要介绍了jquery序列化后的表单值转换成json的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下,希望能帮助到大家。
通过$("#form").serialize()可以获取到序列化的表单值字符串。
例如:
a=1&b=2&c=3&d=4&e=5
通过$("#form").serializeArray()输出以数组形式序列化表单值。
[
{name: 'firstname', value: 'Hello'},
{name: 'lastname', value: 'World'},
{name: 'alias'}, // 值为空
]统统不满足小朋友想得到Json的愿望。堆栈溢出后,找到了一个这样的方法
$.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 || '';
}
});
return o;
};然后通过 $("#form").serializeObject(); 就可以得到Json内容噜。
相关推荐:










