2011-04-20 32 views
2

如何將javascript post方法中的值從一個頁面(home.aspx)傳遞到需要使用window.open方法在新窗口中打開的另一個頁面(post.aspx)。 我的代碼如下:在按鈕單擊事件中調用此JavaScript函數。javascript中的post方法

function postwith() { 
    var myForm = document.createElement("form"); 
    myForm.method="post" ; 
    myForm.action = post.aspx ; 
    var myInput = document.createElement("input") ; 
    myInput.setAttribute("name", 'user'); 

    myForm.appendChild(myInput) ; 

    document.body.appendChild(myForm) ; 
    myForm.submit() ; 
    document.body.removeChild(myForm) ; 
} 


<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="postwith();"/> 

我想訪問post.aspx PGE的頁面加載事件的值(用戶),而這個頁面應該在新窗口中打開。

+0

你有沒有試過給表單元素創建一個「目標」屬性的值「_blank」? – Pointy 2011-04-20 14:10:28

回答

0

您需要確保您使用HTML和BODY標籤發送完整的發佈請求。看到這個SO question on JavaScript POST。我沒有看到你打開一個新的窗口,所以看到上面的帖子,如何做到這一點。

0

您可以通過郵寄傳遞自己的價值觀,只是宣佈在你的HTML表單,然後在javascript做一樣的東西:

document.form.name_of_the_form.submit(); 

當然,這種形式的動作一定是你想要的新頁打開。你也必須在表單中傳遞你的值(通常是type =「hidden」)。

取而代之,您還可以通過GET方法,通過url,使用window.open在javascript中傳遞所需的值。