将form的target设置成和open的name参数一样的值,通过浏览器自动识别实现了将内容post到新窗口中。
function openPostWindow(url, data, name){
var tempForm = document.createElement("form");
tempForm.id="tempForm1";
tempForm.method="post";
tempForm.action=url;
tempForm.target=name;
$.each(data,function(name,value) {
var hideInput = document.createElement("input");
hideInput.type="hidden";
hideInput.name= name;
hideInput.value= value;
tempForm.appendChild(hideInput);
});
if(document.all){
tempForm.attachEvent("onsubmit",function(){
openWindow(name);
}); //IE
}else{
var subObj = tempForm.addEventListener("submit",function(){
openWindow(name);
},false); //firefox
}
document.body.appendChild(tempForm);
if(document.all){
tempForm.fireEvent("onsubmit");
}else{
tempForm.dispatchEvent(new Event("submit"));
}
tempForm.submit();
document.body.removeChild(tempForm);
}
function openWindow(name){
window.open('about:blank',name,"height=600,width=800,top=20,left=20");
}
本文介绍了一种使用JavaScript创建表单并将其提交到新窗口的方法。通过动态生成表单元素,并利用浏览器特性实现跨窗口POST操作。适用于需要在新窗口中加载特定数据的场景。
1328

被折叠的 条评论
为什么被折叠?



