Javascript Post on Form提交打开一个新窗口

https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit告诉你如何提交一个你通过JavaScript创build的表单。 以下是我修改的代码。

var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "test.jsp"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("name", "id"); hiddenField.setAttribute("value", "bob"); form.appendChild(hiddenField); document.body.appendChild(form); // Not entirely sure if this is necessary form.submit(); 

我想要做的是在新窗口中打开结果。 我目前正在使用这样的东西在新窗口中打开一个页面:

 onclick=window.open(test.html,'','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no'); 

 <form target="_blank" ...></form> 

要么

 form.setAttribute("target", "_blank"); 

到你的表单的定义。

如果你想创build和提交你的表单,如在你的问题,你想创build与自定义function的popup窗口,我build议这个解决scheme(我把评论上面我添加的行):

 var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", "test.jsp"); // setting form target to a window named 'formresult' form.setAttribute("target", "formresult"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("name", "id"); hiddenField.setAttribute("value", "bob"); form.appendChild(hiddenField); document.body.appendChild(form); // creating the 'formresult' window with custom features prior to submitting the form window.open('test.html', 'formresult', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no'); form.submit(); 
 var urlAction = 'whatever.php'; var data = {param1:'value1'}; var $form = $('<form target="_blank" method="POST" action="' + urlAction + '">'); $.each(data, function(k,v){ $form.append('<input type="hidden" name="' + k + '" value="' + v + '">'); }); $form.submit(); 

我知道这个基本的方法:

1)

 <input type=”image” src=”submit.png”> (in any place) 

2)

 <form name=”print”> <input type=”hidden” name=”a” value=”<?= $a ?>”> <input type=”hidden” name=”b” value=”<?= $b ?>”> <input type=”hidden” name=”c” value=”<?= $c ?>”> </form> 

3)

 <script> $('#submit').click(function(){ open(”,”results”); with(document.print) { method = “POST”; action = “results.php”; target = “results”; submit(); } }); </script> 

作品!

最简单的工作解决scheme(在Chrome上testing):

 <form action='...' method=post target="result" onsubmit="window.open('','result','width=800,height=400');"> <input name=".."> .... </form>