gpt4 book ai didi

javascript - Window.open并通过post方法传递参数

转载 作者:IT王子 更新时间:2023-10-29 02:47:49 26 4
gpt4 key购买 nike

使用 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)" />   

但是,当我单击此按钮时,站点 test.asp 为空(当然我尝试获取传递值 - Request.Form("b"))。

我该如何解决这个问题,为什么我无法获取传递值?

最佳答案

与其在新窗口中写入表单(这很难正确,使用 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();
}

要发布表单,您可以使用值调用函数,例如 openWindowWithPost('a','b','c');

注意:我改变了与表单名称相关的参数名称,以表明它们不必相同。通常,您会让它们彼此相似,以便更轻松地跟踪这些值。

关于javascript - Window.open并通过post方法传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3951768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com