Https://github.com/marioizquierdo/jquery.serializeJSON
Load Using
<script type= "Text/javascript" src= "jquery.js" ></script><script type= "Text/javascript" src= " Jquery.serializejson.js "></script>
Form Form page code
<form id= "Formdemo" class= "Layui-form" > <table class= "layui-table" lay-even> <colgroup> <col width= > <col width= "$" > <col> </colgroup> < ;tbody> <tr> <th> user name </th> <td id= "UserName" > <div class= "Layui-form-item layui-input-inline" > <input tyep= "text" Name= "Userna Me "class=" Layui-input "> </div> </td> </tr> <tr> <th> Login Password </th> <td id= "Password" > <div class= "Layui-form-item layui-input-inline" > <input tyep= "text" Name= "pass Word "class=" layui-input "> </div> </td> </tr> <tr> <th> user Rights </th> <td id= "role" > <div class= "layui-form- Item Layui-input-inline "> <input type=" text "name=" role "class=" Layui-input "readonly=" true "va Lue= "Admin" > </div> </td> </tr> <tr> <th> phone number </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> User Comments < /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>
Serialization of
<script type= "Text/javascript" > function GetUser () { Console.log ($ (' #formDemo '). Serializejson ()); Console.log (Json.stringify ($ (' #formDemo '). Serializejson ())); Return Json.stringify ($ (' #formDemo '). Serializejson ()); } </script>
$ (' #formDemo '). Serializejson () returns an object body
{userName: "Test", Password: "Test", Role: "admin", Phone: "18888888866", Nickname: "Test"}
Json.stringify ($ (' #formDemo '). Serializejson ()) returns a JSON string
{"UserName": "Test", "Password": "Test", "role": "Administrator", "Phone": "18888888866", "nickname": "Test"}
use Ajax to pass to the background using @requestbody to receive
<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>
to get the values in a key-value pair in JS, you can do the following
<script type= "Text/javascript" > var data = GetUser (); var obj = eval ("(" +data+ ")"); alert (obj.username); </script>
Form form converted to JSON string data