标签:
function serialize(form){
var part=[],
field=null,
i,
len,
option,
optLen,
optValue;
for(i=0,len=form.elements.length;i<len;i++){
field=form.elements[i];
console.log(field.type);
switch (field.type){
case "select-one":
case "select-multiple":
if(field.name.length){
for(var j= 0,optLen=field.options.length;j<optLen;j++){
option=field.options[j];
if(option.selected){
if(option.hasAttribute){
optValue=(option.hasAttribute(‘value‘)?option.value:option.text);
}else{
optValue=(option.attribute(‘value‘).specified?option.value:option.text);
}
part.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 :
if(field.name.length){
part.push(encodeURIComponent(field.name)+‘=‘+encodeURIComponent(field.value));
}
}
}
return part.join("&");
}
reference :高程14章
标签:
原文地址:http://www.cnblogs.com/wz0107/p/4927409.html