首页 > 代码库 > 分享一个 jquery serializeArray()序列化方法

分享一个 jquery serializeArray()序列化方法

http://www.365mini.com/page/jquery-serializearray.htm
http://www.365mini.com/diy.php?f=jquery-serializearray-demo

<form name="myForm" action="http://www.365mini.com" method="post">
    <input name="uid" type="hidden" value="http://www.mamicode.com/1" />
    <input name="username" type="text" value="http://www.mamicode.com/张三" />
    <input name="password" type="text" value="http://www.mamicode.com/123456" />
    <select name="grade" id="grade">
        <option value="http://www.mamicode.com/1">一年级</option>
        <option value="http://www.mamicode.com/2">二年级</option>
        <option value="http://www.mamicode.com/3" selected="selected">三年级</option>
        <option value="http://www.mamicode.com/4">四年级</option>
        <option value="http://www.mamicode.com/5">五年级</option>
        <option value="http://www.mamicode.com/6">六年级</option>
    </select>
    <input name="sex" type="radio" checked="checked" value="http://www.mamicode.com/1" />男
    <input name="sex" type="radio" value="http://www.mamicode.com/0" />女
    <input name="hobby" type="checkbox" checked="checked" value="http://www.mamicode.com/1" />游泳
    <input name="hobby" type="checkbox" checked="checked" value="http://www.mamicode.com/2" />跑步
    <input name="hobby" type="checkbox" value="http://www.mamicode.com/3" />羽毛球
    <input name="btn" id="btn" type="button" value="http://www.mamicode.com/点击" />
</form>

 

var formArray = $("form").serializeArray();
/* 以下是序列化后的结果数组formArray的内容:
[
	{ name: "uid", value: "1" },
	{ name: "username", value: "张三" },
	{ name: "password", value: "123456" },
	{ name: "grade", value: "3" },
	{ name: "sex", value: "1" },
	{ name: "hobby", value: "1" },
	{ name: "hobby", value: "2" }
];
*/

alert( formArray[0].name + "=" + formArray[0].value );

var result = $(":text, select, :checkbox").serializeArray();
/* 以下是序列化后的结果数组result的内容:
[ 
	{ name: "username", value: "张三" }, 
	{ name: "password", value: "123456" },
	{ name: "grade", value: "3" },
	{ name: "hobby", value: "1" },
	{ name: "hobby", value: "2" }
];
*/

alert( result[0].name + "=" + result[0].value );

 

 

分享一个 jquery serializeArray()序列化方法