Window.open并通过post方法传递参数 [英] Window.open and pass parameters by post method

查看:3868
本文介绍了Window.open并通过post方法传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用window.open方法我打开带有参数的新站点,我必须通过post方法传递。我找到了解决方案,但遗憾的是它不起作用。这是我的代码:

With window.open method I open new site with parameters, which I have to pass by post method.I've found solution, but unfortunately it doesn't work. This is my code:

<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>  

并且通过以下方式调用函数:

And call function by:

<input id="Button1" type="button" value="Pass values" onclick="openWindowWithPost('test.asp','',keys,values)" />   

但是,当我点击这个按钮时,网站test.asp是空的(当然我试试获取传递值 - Request.Form(b))。

But, when I click on this button, the site test.asp is empty (of course I try get pass values - Request.Form("b")).

我怎么能解决这个问题,为什么我无法获得传递值?

How could I solve this problem, why I can't get pass values?

推荐答案

而不是将表单写入新窗口(通过HTML代码中的值编码来获取正确的方法很棘手) ),只需打开一个空窗口并将表格发布到它。

Instead of writing a form into the new window (which is tricky to get correct, with encoding of values in the HTML code), just open an empty window and post a form to it.

示例:

<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>



编辑:



设置值在动态的形式,您可以这样做:

To set the values in the form dynamically, you can do like this:

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

To post the form you call the function with the values, like openWindowWithPost('a','b','c');.

注意:我改变了关系中的参数名称表单名称表明它们不必相同。通常你会让它们彼此相似,以便更容易跟踪这些值。

Note: I varied the parameter names in relation to the form names to show that they don't have to be the same. Usually you would keep them similar to each other to make it simpler to track the values.

这篇关于Window.open并通过post方法传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆