前言:
之前是每个input输入框去单个获取值,然后再打包给后台
有比较快捷的方式是用jq已有方式进行打包,不用再每个输入框去单个获取值,
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<form action="" method="post">
性别:<input type="text" name="sex" value="男"/><br />
年龄:<input type="text" name="age" value="18"/><br />
地址:<input type="text" name="where" value="sichuan"/><br />
电话:<input type="text" name="phone" value=""/><br />
</form>
<button>提交</button>
<script src="jquery.min.js"></script>
<script>
init();
function init(){
fun1();
fun2();
}
function fun1(){
var data=$('form').serializeArray();
console.log(data);//以数组方式收集
}
function fun2(){
var data={};
$('form').serializeArray().map(function(x){data[x.name]=x.value});
console.log(data);//已对象方式收集
}
// 提交
$("body").on("click","button",function(){
init();
})
</script>
</body>
</html>
比较常用的是已对象方式进行收集