JavaScript模拟表单(带数组的复杂数据结构)提交
function test(){
var typeArray = new Array();
typeArray.push("mm");
typeArray.push("gg");
var demoarry = new Array();
demoarry.push("dd");
demoarry.push("qq");
typeArray.push(demoarry);
console.log(typeArray);
var id = 0;
var data = {
id: id ,
type: typeArray,
demoarry : demoarry
};
httpPostUrlExt("http://www.baidu.com",data);
}
function httpPostUrlExt(url, data) {
var temp = document.createElement("form");
temp.action = url;
temp.method = "post";
temp.style.display = "none";
for (var x in data) {
if(Object.prototype.toString.call(data[x]) === '[object Array]' ) {
var arr = data[x];
while(arr.length){
var opt = document.createElement("textarea");
opt.name = x;
opt.value = arr.pop();
temp.appendChild(opt);
}
} else {
var opt = document.createElement("textarea");
opt.name = x;
opt.value = data[x];
temp.appendChild(opt);
}
}
document.body.appendChild(temp);
console.log(temp);
//temp.submit();
return temp;
}