[英]prompting a user when leaving page
在我的頁面上,我有一個名為cbOrderSpareCustomer的組合框。 默認情況下,所選索引設置為0。
當用戶更改它時,我認為該頁面包含數據,並且當用戶決定離開該頁面時,我想提醒他讓他知道數據將丟失。
我已經看過很多關於此的文章,但是我對javascript還是很陌生,所以我可以使用一些幫助。
我了解我必須使用:
<script>
window.onbeforeunload= function() { return "Custom message here"; };
</script>
但是,如何使它與組合框一起使用?
如if cbOrderSpareCustomer.selectedIndex > 0 then prompt else just continue.
我也想阻止它在每次回發中顯示提示。
我想看一個例子。
您會在客戶端的下拉列表中查看索引,您的代碼可能如下所示:
<script>
window.onbeforeunload= function()
{
if(document.getElementById('<%=cbOrderSpareCustomer.ClientID%>').selectedIndex > 0)
{
return "Custom message here";
}
else
{
return true;
}
};
</script>
我認為...
window.onbeforeunload = function(e) {
if (document.getElementById('cbOrderSpareCustomer').selectedIndex > 0) {
if (!confirm('Are you sure')) {
return false;
}
}
}
請按照以下步驟進行操作
onchange
屬性添加到服務端的組合框中: <select id="cbOrderSpareCustomer" onchange="setDirty()">
如下定義您的JavaScript
var isDirty = true;
function setDirty() { isDirty = true; } window.onbeforeunload = function () { if (isDirty) { return confirm('Your unsaved changes will be lost'); } }
如果您使用的是JQuery
<script type="text/javascript">
$(window).bind('beforeunload', function () {
if ($("select[name='cbOrderSpareCustomer'] option:selected").index() > 0) {
return 'Your data will be lost.';
}
});
</script>
您不希望其他返回true,否則它仍會提示(至少IE提示)
我的解決方案:
<script type="text/javascript"
function setDirty() {
window.onbeforeunload = function () {
return 'The data will be lost if you leave this page!'
}
}
</script>
將屬性onCange =“ setDirty()”添加到下拉列表中。
謝謝大家的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.