2008-11-06 69 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鏈接。真的,這應該是一個按鈕,而不是一個鏈接。

避免這種東西,除非你有特定的理由。根據你實際嘗試做什麼,通常有更好的方法。

相關問題