.serialize
html:
<form action="">
First name: <input type="text" name="FirstName" value="Bill" /><br />
Last name: <input type="text" name="LastName" value="Gates" /><br />
</form>
<button>表单序列化</button>
<div></div>
js:
$("button").click(function(){
var a=$("form").serialize();
/*var a=$(":checkbox,:radio").serializeArray();*/
console.log(a);
})
输出结果:
FirstName=Bill&LastName=Gates
.serializeArray输出结果为json格式
html:
<form action="">
<input type="radio" name="radiobutton" value="radiobutton" checked> 喜欢
<input type="radio" name="radiobutton" value="radiobutton"> 不喜欢
<input type="radio" name="radiobutton" value="radiobutton"> 无所谓<br>
<input type="checkbox" name="checkbox1" value="checkbox"> 跑步
<input type="checkbox" name="checkbox2" value="checkbox"> 打球
<input type="checkbox" name="checkbox3" value="checkbox"> 登山
<input type="checkbox" name="checkbox4" value="checkbox"> 健美<br>
</form>
<button>表单序列化</button>
<div></div>
var a=$(":checkbox,:radio").serializeArray();
console.log(a);
输出结果: