表单序列化 Form serialize

 
   
序列化
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。

function
serializeForm(whichForm){
var parts = new Array();
var field = null;
for (var i = 0, len = whichForm.elements.length; i < len; i++) {
field = whichForm.elements[i];
switch (field.type) {
case "select-one":
case "select-multipe":
for (var j = 0, optLen = field.options.length; j < optLen; j++) {
var option = field.options[j];

if (option.selected) {
var optValue = "";
if (option.hasAttribute) {
optValue = (option.hasAttribute("value") ? option.value : option.text);
}
else {
optValue = (option.attributes["value"].specified ? option.value : option.text);
}
parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
}
}
break;
case undefined:
case "file":
case "submit":
case "reset":
case "button":
break;
case "radio":
case "checkbox":
if (!field.checked) {
break;
}
default:
parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));
}
}
return parts.join("&");
}

你可能感兴趣的:(Serialize)