如何用javascript以post方式提交form表单
1个回答
展开全部
下面是用js动态创建form然后进行post提交的方式,参数url是表单提交的去向,参数params是给去向传递的参数(JSON结构),参数target设置何处打开链接文档(可选):
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);
}
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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询