フォーム送信でのJavascriptの投稿新しいウィンドウが開きます

StackOverflow https://stackoverflow.com/questions/178964

  •  05-07-2019
  •  | 
  •  

質問

https://stackoverflow.com/questions/133925/javascript-post -request-like-a-form-submit は、投稿を介してJavaScriptで作成したフォームを送信する方法を示しています。以下は私の修正したコードです。

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");

フォームの定義に。

他のヒント

質問にあるように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();
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>

動作!

フローウィンドウの最も簡単な作業ソリューション(Chromeでテスト済み):

<form action='...' method=post target="result" onsubmit="window.open('','result','width=800,height=400');">
    <input name="..">
    ....
</form>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top