jQuery序列化表单为JSON对象
2015-06-02 15:58
666 查看
<form id="myform"> <table> <tr> <td>姓名:</td> <td> <input type="text" name="name" /> </td> </tr> <tr> <td>性别:</td> <td> <input type="radio" name="sex" value="1"> 男 <input type="radio" name="sex" value="0"> 女 </td> </tr> <tr> <td>年龄:</td> <td> <select name="age"> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> </select> </td> </tr> <tr> <td>爱好</td> <td> <input type="checkbox" value="basketball" name="hobby">篮球 <input type="checkbox" value="volleyball" name="hobby">排球 <input type="checkbox" value="football" name="hobby">足球 <input type="checkbox" value="earth" name="hobby">地球 </td> </tr> <tr> <td colspan="2"> <input type="button" id="ajaxBtn" value="提交" /> </td> </tr> </table> </form>
<script type="text/javascript"> $(function() { $("#ajaxBtn").click(function() { var params = $("#myform").serializeObject(); //将表单序列化为JSON对象 console.info(params); }) }) $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name]) { 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; } </script>上述serializeObject方法是将form序列化为JSON对象
相关文章推荐
- jQuery学习笔记2--表格内容筛选
- jQuery Callback 方法
- jQuery 停止动画
- jquery 使用attr() 函数对复选框无效的原因,javascript那些事儿——properties和attributes
- jquery tmpl
- jquery实现加载更多
- ExtJS结合WCF/Jquery结合WCF 分别开发的非常优秀的网页聊天工具,持续更新
- jQuery学习笔记1--表格展开关系
- jQuery 效果- 动画
- JQuery 常用方法经典总结
- jQuery.extend()方法和jQuery.fn.extend()方法源码分析
- jQuery插件学习(一)
- 基于jQuery悬停图片变色放大特效
- jQuery 效果 - 滑动
- jQuery 效果 - 淡入淡出
- jquery 不支持$.browser
- 当使用servlet输出json时,浏览器端jquery的ajax遇到parse error的问题
- 强大的响应式jQuery消息通知框和信息提示框插件
- jQuery插件开发及jQuery.extend函数详解和jQuery.fn与jQuery.prototype区别
- jQuery设置按钮被点击状态