serializeArray()和.serialize()的区别、联系
<form id='addForm' action='UserAdd.action' type='post'> <label for='uname'>用户名</label>:<input type='text' name='uname' id='uname'><br> <label for='mobileIpt'>手机号:</label><input type='text' name='mobileIpt' id='mobileIpt'><br> <label for='birthday'>生日:</label><input type='text' name='birthday'><br> <input type='button' value='提交' onclick='addUser()'> </form>
serializeArray() json数组
--------$.type()可以判断数据类型
$('#addForm').serializeArray(); //返回数据结构,是json数组,每个对像分别name和value为key,代表这个表单元素的name和value [ {"name":"uname","value":""}, {"name":"mobileIpt","value":""}, {"name":"birthday","value":""} ]
取法:
var data=$('#addForm').serializeArray();
$.each(data, function(index, field){ $("#results").append(field.name+":"+field.value+" "); });
serialize() 序列化表单元素为字符串
var arr = $('#addForm').serialize(); $.param(arr); "uname=alice&mobileIpt=110&birthday=1983-05-12"
联系:相互转换
var arr = $('#addForm').serializeArray();
$.param(arr);
"uname=alice&mobileIpt=110&birthday=1983-05-12"