javascript - formdata - xmlhttprequest




Javascript在表單上發布提交打開一個新窗口 (4)

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

到你表單的定義。


如果你想創建並提交你的問題,你的問題是你的形式,並且你想創建自定義功能的彈出窗口,我建議這個解決方案(我把註釋放在我添加的上面):

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

流動窗口最簡單的工作解決方案(在Chrome上測試):

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

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




post