表单上的Javascript帖子提交打开一个新窗口

149 html javascript post

/sf/ask/9374781/向您展示如何通过邮件提交您通过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();
Run Code Online (Sandbox Code Playgroud)

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

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

lig*_*t78 219

<form target="_blank" ...></form>
Run Code Online (Sandbox Code Playgroud)

要么

form.setAttribute("target", "_blank");
Run Code Online (Sandbox Code Playgroud)

到你的表格的定义.


Mar*_*mic 130

如果你想从你的问题中创建和提交你的表单,你想要创建具有自定义功能的弹出窗口我建议这个解决方案(我把评论放在我添加的行之上):

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();
Run Code Online (Sandbox Code Playgroud)

  • +1比接受的答案更好,因为它实际上将结果放在弹出窗口中,并带有OP想要的选项. (18认同)
  • @SaurabhNanda据我所知,``上元件form` target`属性**未弃用**在[HTML 4.01过渡](http://www.w3.org/TR/REC-html40/present/ frames.html#h-16.3)并且显然是留在[HTML 5](http://www.w3.org/TR/html5/the-form-element.html#the-form-element). (3认同)

luc*_*nov 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();
Run Code Online (Sandbox Code Playgroud)