jquery confirm and exit functionality

﹥>﹥吖頭↗ 提交于 2019-12-13 03:12:14

问题


In the html page shown,where is window.onbeforeunload = confirmExit code to be placesd so that when validate div is shown the alert message does not appear when the user clicks on cancel or save button.And if the user clicks or any other link the alert message should be shown

If any other link is clicked makeajaxcall() function should be called and then continue

    <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:


In $(document).ready(), your syntax is wrong,

you forgot to write function() in .ready(function (){ .... });

$(document).ready(function() {
   if(load_flag == 2) {
     $("#validate").css({'display':'block'});
     $("#start").css({'display':'none'});
     $("#confirm").css({'display':'none'});
     window.onbeforeunload = confirmExit ;
   }
});


来源:https://stackoverflow.com/questions/3793662/jquery-confirm-and-exit-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!