2017-07-25 73 views
0

我希望用戶在離開網頁時收到警告。我還希望用戶在按某個按鈕提交內容時獲得不同的警報。在你離開網頁時,一切正常,但當你按下提交時,你會看到兩條警告消息。我如何得到這個只顯示一個警報?如何在離開頁面時警告用戶,而不是在按下按鈕時警告用戶?

<asp:Button Style="display: inline-block; margin-left: 120px; margin-top: 10px;" ID="SendEmail" runat="server" CssClass="btn-flat success" Text="Send email" OnClick="btnUpdate_click" Visible="true" Enabled="True" OnSubmit="window.onbeforeunload = false;" OnClientClick="if (!confirm('Are you sure you want to send this email?')) return false;"/> 
    <script> 

     window.onbeforeunload = function() { 
     return "Do you want to leave this page?"; 
    } 

我用window.onbeforeunload離職的網頁,當你按下按鈕提交了的OnClientClick調用。我沒有Javascript的經驗,所以我不知道我在做什麼。

+1

只需創建一個標誌,以保持按鈕是否被點擊或不和,並顯示像'如果(標誌){警報的消息(「你想離開這個頁面?「); }' –

回答

0

您可以在點擊提交時設置一個變量。所以,如果這是真的,不要顯示window.onbeforeunload消息。

做這樣的事情。

<asp:Button Style="display: inline-block; margin-left: 120px; margin-top: 10px;" ID="SendEmail" runat="server" 
       CssClass="btn-flat success" Text="Send email" OnClick="btnUpdate_click" Visible="true" Enabled="True" 
       OnSubmit="window.onbeforeunload = false;" OnClientClick="return Confirm();" /> 

和JS

var IsSubmit = false; 

function Confirm() 
{ 
    IsSubmit = true; 
    if (!confirm('Are you sure you want to send this email?')) 
     return false; 
} 

if(IsSubmit != true) 
{ 
    window.onbeforeunload = function() { 
     return "Do you want to leave this page?"; 
    } 
} 

感謝

+0

這是非常糟糕的結構,不會工作... –

相關問題