Reprinted from: http://blog.csdn.net/itmyhome1990/article/details/42099885
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ include file= "/web-inf/jsp/common.jsp"%>
<! DOCTYPE HTML >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<script type= "Text/javascript" src= "${basepath}/system/lib/jquery-1.11.3/jquery.min.js" ></script>
<script type= "Text/javascript" language= "JavaScript" >
(function ($) {
$.fn.serializejson = function () {
var jsonData1 = {};//Declare an empty map
var Serializearray = This.serializearray ();
//First converted to {"id": ["["], "+"], "name": ["AAA", "BBB"], "pwd": ["Pwd1", "Pwd2"]} This form
$ (Serializearray). each (function () {
if (Jsondata1[this.name]) {//If map has id\name\pwd key
if ($.isarray (Jsondata1[this.name])) {//If key corresponds to value is an array
Jsondata1[this.name].push (This.value);
} else {
Jsondata1[this.name] = [Jsondata1[this.name], this.value];//Set key corresponding to value to array
}
} else {
Jsondata1[this.name] = This.value;
}
});
//Turn to [{"id": "Triple", "Name": "AAA", "pwd": "PWD1"},{"id": "+", "name": "BB", "pwd": "Pwd2"}] form
var vcount = 0;
//Calculate the maximum length of the array inside the JSON
For (var item in jsonData1) {
var tmp = $.isarray (Jsondata1[item])? Jsondata1[item].length:1;
Vcount = (tmp > Vcount)? Tmp:vcount;
}
if (Vcount > 1) {
var jsonData2 = new Array ();
for (var i = 0; i < Vcount; i++) {
var jsonobj = {};
For (var item in jsonData1) {
Jsonobj[item] = Jsondata1[item][i];
}
Jsondata2.push (Jsonobj);
}
Return json.stringify (JSONDATA2);
}else{
Return "[" + json.stringify (JSONDATA1) + "]";
}
};
}) (JQuery);
function Submituserlist_4 () {alert ("OK");
var jsonstr = $ ("#form1"). Serializejson ();
Console.log ("jsonstr:\r\n" + jsonstr);
/* $.ajax ({
URL: "/user/submituserlist_4",
type: "POST",
contentType: ' Application/json;charset=utf-8 ',//Set request header information
dataType: "JSON",
Data:jsonstr,
success:function (data) {
alert (data);
},
Error:function (res) {
alert (res.responsetext);
}
}); */
}
</script>
<body>
<form id= "Form1" >
id:<input type= "text" name= "ID" ><br/>
username:<input type= "text" name= "name" ><br/>
password:<input type= "text" name= "pwd" ><br/><br/>
id:<input type= "text" name= "ID" ><br/>
username:<input type= "text" name= "name" ><br/>
password:<input type= "text" name= "pwd" ><br/><br/>
<input type= "button" value= "Submit" onclick= "Submituserlist_4 ()" >
</form>
</body>
form is serialized as JSON (input only)