js模拟表单提交

postcall( '/index.php', { UserName: 'UserName', }); function postcall( url, params, target){ var tempform = document.createElement("form"); tempform.action = url; tempform.method = "post"; tempform.style.display="none" if(target) { tempform.target = target; } for (var x in params) { var opt = document.createElement("input"); opt.name = x; opt.value = params[x]; tempform.appendChild(opt); } var opt = document.createElement("input"); opt.type = "submit"; tempform.appendChild(opt); document.body.appendChild(tempform); tempform.submit(); document.body.removeChild(tempform); }
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

哈哈 以前写过一个postOpen

//发送POST请求跳转到指定页面 // var params = { // "username": "admin", // "password": "123" // }; // postOpen("", params); function postOpen(URL, PARAMS) { var temp = document.createElement("form"); temp.action = URL; temp.method = "post"; temp.target = "_blank"; //1.跳转新窗口需要设置该参数 2.当前页跳转可以去掉该参数 temp.style.display = "none"; for (var x in PARAMS) { var opt = document.createElement("textarea"); opt.name = x; opt.value = PARAMS[x]; temp.appendChild(opt); } document.body.appendChild(temp); temp.submit(); document.body.removeChild(temp); }
2年前 评论