jQuery序列化后的表单值转换成Json

小朋友有一个表单,他想以Json的方式获取到表单的内容。小朋友尝试了以下方式。

通过$("#form").serialize()可以获取到序列化的表单值字符串。

例如:

rush:js;"> a=1&b=2&c=3&d=4&e=5

通过$("#form").serializeArray()输出以数组形式序列化表单值。

rush:js;"> [ {name: 'firstname',value: 'Hello'},{name: 'lastname',value: 'World'},{name: 'alias'},// 值为空 ]

统统不满足小朋友想得到Json的愿望。堆栈溢出后,找到了一个这样的方法

rush:js;"> $.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。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...