2017-07-25 72 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

这是非常糟糕的结构,不会工作... –

相关问题