jquery确认并退出功能

时间:2010-09-25 11:51:01

标签: jquery jquery-ui jquery-selectors jquery-validate

在显示的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>

1 个答案:

答案 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 ;
   }
});