在显示的html页面中,window.onbeforeunload = confirmExit
代码放在哪里,这样当显示验证div时,当用户点击取消或保存按钮时,不会显示警告消息。如果用户点击或任何其他链接应显示警告信息
如果单击任何其他链接,则应调用makeajaxcall()函数,然后继续
<div id="start" ></div>
<div id="validate" >
<input type="button" /value="cancel" onclick="window.locatcation='cancel()'">
<input type="button" /value="save" onclick="save()">
</div>
<div id="confirm" ></div>
<input type="hidden" name="localchanges" id="localchanges" value="1">
<script>
function cancel()
{
$('lovalchanges').val('0');
makeajaxcall();
}
function makeajxcall()
{
send request to server and get response
}
$(document).ready({
if(load_flag == 2)
{
$("#validate").css({'display':block});
$("#start").css({'display':none});
$("#confirm").css({'display':none});
window.onbeforeunload = confirmExit ;
}
});
function confirmExit()
{
var ele = document.getElementById ("localchanges") ;
if (ele.value == "1")
return "You have not saved any change made . Discard Changes ?" ;
}
</script>
答案 0 :(得分:0)
在$(document).ready()
中,您的语法错误,
您忘记在function()
.ready(function (){ .... });
$(document).ready(function() {
if(load_flag == 2) {
$("#validate").css({'display':'block'});
$("#start").css({'display':'none'});
$("#confirm").css({'display':'none'});
window.onbeforeunload = confirmExit ;
}
});