form表单转换为Json数据
来源:互联网 发布:怎样走路没声音 知乎 编辑:程序博客网 时间:2024/06/09 08:16
项目主页
https://github.com/marioizquierdo/jquery.serializeJSON
效果图
加载使用
<script type="text/javascript" src="jquery.js"></script><script type="text/javascript" src="jquery.serializejson.js"></script>
form表单页面代码
<form id="formDemo" class="layui-form"> <table class="layui-table" lay-even> <colgroup> <col width="100"> <col width="200"> <col> </colgroup> <tbody> <tr> <th>用户名</th> <td id="userName"> <div class="layui-form-item layui-input-inline"> <input tyep="text" name="userName" class="layui-input"> </div> </td> </tr> <tr> <th>登录密码</th> <td id="password"> <div class="layui-form-item layui-input-inline"> <input tyep="text" name="password" class="layui-input"> </div> </td> </tr> <tr> <th>用户权限</th> <td id="role"> <div class="layui-form-item layui-input-inline"> <input type="text" name="role" class="layui-input" readOnly="true" value="管理员"> </div> </td> </tr> <tr> <th>手机号</th> <td id="phone"> <div class="layui-form-item layui-input-inline"> <input tyep="text" name="phone" class="layui-input"> </div> </td> </tr> <tr> <th>用户备注</th> <td id="nickName"> <div class="layui-form-item layui-input-inline"> <input tyep="text" name="nickName" class="layui-input"> </div> </td> </tr> </tbody> </table> </form>
序列化
<script type="text/javascript"> function getUser(){ console.log($('#formDemo').serializeJSON()); console.log(JSON.stringify($('#formDemo').serializeJSON())); return JSON.stringify($('#formDemo').serializeJSON()); }</script>
$('#formDemo').serializeJSON()返回的是一个Object体
{userName: "test", password: "test", role: "管理员", phone: "18888888866", nickName: "测试"}
JSON.stringify($('#formDemo').serializeJSON())返回的是JSON字符串
{"userName":"test","password":"test","role":"管理员","phone":"18888888866","nickName":"测试"}
使用Ajax传递给后台使用@RequestBody进行接收
<script type="text/javascript"> function addUser(){ var data = getUser(); $.ajax({ type: "POST", url: "${ctx}/auth/user/create", data: data, dataType: "json", contentType:"application/json", success: function(data){ alert(data.message); if(data.code==0){ layer.closeAll(); window.location.href="${ctx}/auth/users"; } } }); } </script>
若要在JS中获取键值对中的值可以进行如下处理
<script type="text/javascript"> var data = getUser(); var obj = eval("("+data+")"); alert(obj.userName); </script>
阅读全文
0 0
- form表单数据转换为json对象
- form表单数据转换为json对象
- form表单数据转换为json对象
- form表单数据转换为json对象
- form表单转换为Json数据
- form表单转换为json
- form表单的数据转换为json对象
- form表单转换为json对象
- 表单的数据转换为json对象
- form数组转换为json
- 获取页面表单数据转换为json对象
- 重置form表单为json对象
- HTML表单Form转化为JSON-yellowcong
- C# 将json字符串转换为键值对形式,用于执行form表单形式的请求
- form表单转成json个数数据
- form表单提交json格式数据
- 将后台JSON数据填充Form表单
- 前端基础-09-form表单&json数据
- [PAT-乙级]1048.数字加密
- poj 2586 Y2K Accounting Bug Time Limit
- mongoDB 多重数组查询 AngularJS绑定显示 nodejs
- 517求职准备
- 杭电2034
- form表单转换为Json数据
- bzoj 2157 旅游
- markdown编辑语法
- bootstrap中 炫酷按钮的实现(自我总结)
- IDEA 2017破解 license server激活
- hashMap 环的出现分析
- 软考路——算法其实很简单
- [PAT-乙级]1049.数列的片段和
- Unity FixedJoint