使用window.open方法,我打开带有参数的新站点,我必须通过post方法传递这些参数。我已经找到了解决方案,但不幸的是它不起作用。这是我的代码:
<script type="text/javascript">
function openWindowWithPost(url,name,keys,values)
{
var newWindow = window.open(url, name);
if (!newWindow) return false;
var html = "";
html += "<html><head></head><body><form id='formid' method='post' action='" + url +"'>";
if (keys && values && (keys.length == values.length))
for (var i=0; i < keys.length; i++)
html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</sc"+"ript></body></html>";
newWindow.document.write(html);
return newWindow;
}
</script>
接下来,我创建数组:
<script type="text/javascript">
var values= new Array("value1", "value2", "value3")
var keys= new Array("a","b","c")
</script>
并通过以下方式调用函数:
<input id="Button1" type="button" value="Pass values" onclick="openWindowWithPost('test.asp','',keys,values)" />
但是,当我单击此按钮时,站点值为空(当然,我尝试使用get pass test.asp - Request.Form("b")
)。
我怎么才能解决这个问题,为什么我不能得到传递的值?
发布于 2010-10-17 11:02:15
您只需打开一个空窗口并在其中发布一个表单,而不是将表单写入到新窗口中(这很难正确处理,需要对HTML代码中的值进行编码)。
示例:
<form id="TheForm" method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something" />
<input type="hidden" name="more" value="something" />
<input type="hidden" name="other" value="something" />
</form>
<script type="text/javascript">
window.open('', 'TheWindow');
document.getElementById('TheForm').submit();
</script>
编辑:
要动态设置表单中的值,可以这样做:
function openWindowWithPost(something, additional, misc) {
var f = document.getElementById('TheForm');
f.something.value = something;
f.more.value = additional;
f.other.value = misc;
window.open('', 'TheWindow');
f.submit();
}
要post表单,您需要调用带有值的函数,如openWindowWithPost('a','b','c');
。
注意:我改变了与表单名称相关的参数名称,以显示它们不必相同。通常,您会使它们彼此相似,以便更容易跟踪值。
发布于 2013-09-03 17:05:07
由于您希望在javascript中包含整个表单,而不是将其写入标记中,因此您可以这样做:
let windowName = 'w_' + Date.now() + Math.floor(Math.random() * 100000).toString();
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "openData.do");
form.setAttribute("target", windowName);
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "message");
hiddenField.setAttribute("value", "val");
form.appendChild(hiddenField);
document.body.appendChild(form);
window.open('', windowName);
form.submit();
发布于 2015-04-03 15:30:50
我完全同意上面发布的mercenary的答案,并为我创建了这个函数,它对我来说是有效的。这不是一个答案,它是mercenary对上面帖子的评论
function openWindowWithPostRequest() {
var winName='MyWindow';
var winURL='search.action';
var windowoption='resizable=yes,height=600,width=800,location=0,menubar=0,scrollbars=1';
var params = { 'param1' : '1','param2' :'2'};
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", winURL);
form.setAttribute("target",winName);
for (var i in params) {
if (params.hasOwnProperty(i)) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = i;
input.value = params[i];
form.appendChild(input);
}
}
document.body.appendChild(form);
window.open('', winName,windowoption);
form.target = winName;
form.submit();
document.body.removeChild(form);
}
https://stackoverflow.com/questions/3951768
复制相似问题