本文主要介绍了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内容噜。
相关推荐:
以上就是jQuery序列化后的表单值转换成Json实例讲解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号