2008-11-06 73 views
2
<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" > 
</form> 
<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="http://stackoverflow.com/" > 
</form> 
<a onclick="test(event, '1'); " href="#" >Click Here</a> 
<script> 
    function test(event, id){ 
     document.getElementById("frm_"+id).submit; 
     document.getElementById("tgt_"+id).submit; 
    } 
</script> 

是否可以打开新选项卡/窗口并更改当前页面?如何打开新选项卡并更改当前页

回答

1
<form id="frm_1" name="frm_1" target="_self" method="POST" action="local_page.php" > 
<input type="hidden" name="vital_param" value="<?= $something ?>"> 
</form> 

<form id="tgt_1" name="tgt_1" target="_blank" method="POST" action="http://stackoverflow.com/" > 
</form> 

<button type="submit" onclick="test(event, '1'); " >Click Here</button> 

<script> 
    function test(event, id){ 
    window.open(document.getElementById("tgt_"+id).action, "_blank"); 
    setTimeout('document.getElementById("frm_'+id+'").submit();', 1000); 

    return true; 
    } 
</script> 

tgt保留作为目标url的来源,可能是数组或属性。 没有setyTimeout()浏览器停留在当前页面(FF/IE)上。

1

据我所知,无法一次提交两种表格。但是,由于您使用的是PHP,为什么不看看cURL库?它可以让你发送POST和GET请求并用PHP解析结果。

要回答这个问题的称号,如果你只是想打开两个页面与一个点击,你可以做这样的(原谅的内嵌JavaScript):

<a 
    href="http://www.google.com" 
    target="_blank" 
    onclick="document.location.href='http://www.yahoo.com'" 
>Click here to open Google in a new window and yahoo in this window</a> 
0

您应该使用的窗口。打开打开新页面,然后提交tabchange,例如

<form id="frm_1" name="frm_1" target="_self" method="GET" action="local_page.php" > 
</form> 
<a onclick="test(event, '1'); " href="#" >Click Here</a> 
<script> 
    function test(event, id){ 
     window.open("http://stackoverflow.com/", "_blank"); 
     document.getElementById("tgt_"+id).submit(); 
    } 
</script> 
0

使用表单元素上的'target'属性使它们提交给eg。一个新窗口或一个iframe,而不用重新加载当前页面(打破任何其他表单提交)。

您的a.onclick也应返回false来停止正在遵循的href链接。真的,这应该是一个按钮,而不是一个链接。

避免这种东西,除非你有特定的理由。根据你实际尝试做什么,通常有更好的方法。

相关问题