如何在特定按钮上设置 Onbeforeunload 功能? 例如,我有 3 个按钮。
<div>
<asp:Button ID="btnBack" runat="server" Text="Back" CssClass="po-font" Height="30px"/>
<asp:Button ID="btnSumbit" runat="server" Text="Submit" CssClass="po-font" Height="30px"/>
<asp:Button ID="btnSaveToDraft" runat="server" Text="Save To Draft" CssClass="po-font" Height="30px"/>
</div>
在 javascript 上,我做了类似的事情:
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "Are you sure you want to leave this page? Any unsaved progress will be lost";
}
</script>
虽然该功能可以正常工作,但我想在可能位于“后退”按钮上的特定按钮中指定该功能。我做了类似的事情。
<script type="text/javascript">
function confirmExit()
{
return "Are you sure you want to leave this page? Any unsaved progress will be lost";
}
$('#btnBack').live('click', function () {
window.onbeforeunload = confirmExit;
});
</script>
但 Id 不起作用。这个怎么做?有任何想法吗?我只想触发指定按钮上的功能。帮助我。
最佳答案
使用$('<%=btnBack.ClientID%>').click(function(){...});
因为 ASP.NET 在自己的客户端上添加了控件 id 前缀,而 html 呈现的 id 可能看起来像 ct100$btnBack。
关于javascript - 允许 ASP.NET Framework 上的特定按钮上的 Javascript Onbeforeunload 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26249620/