154

JavaScript 发布请求(如表单提交)向您展示了如何提交您通过 JavaScript 中的 POST 创建的表单。下面是我修改后的代码。

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');
4

5 回答 5

226

添加

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

或者

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

根据您的表单定义。

于 2008-10-07T15:23:31.410 回答
134

如果您想按照问题中的方式从 Javascript 创建和提交表单,并且想要创建具有自定义功能的弹出窗口,我建议使用此解决方案(我将评论放在我添加的行上方):

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();
于 2008-10-08T00:54:56.107 回答
8
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();
于 2012-09-24T20:55:06.947 回答
2

流窗口最简单的工作解决方案(在 Chrome 上测试):

<form action='...' method=post target="result" onsubmit="window.open('','result','width=800,height=400');">
    <input name="..">
    ....
</form>
于 2017-01-07T20:43:25.043 回答
1

我知道这个基本方法:

1)

<input type=”image” src=”submit.png”&gt; (in any place)

2)

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

3)

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

作品!

于 2010-03-06T01:21:29.167 回答