2017-08-28 60 views
0

我尝试显示一条消息,当用户编辑一些领域,尝试去到另一个网页,或点击接近jQuery的:离开页面,而在编辑模式下

消息出现在页面之前确认对话框

本页面要求您确认您要离开 - 您输入的数据可能未保存。

我用这个代码:

$('#form').data('serialize',$('#form').serialize()); 
    // On load save form current state 

$(window).bind('beforeunload', function(e){ 
    if($('#form').serialize()!=$('#form').data('serialize'))return true; 
    else e=null; 
    // i.e; if form state change show box not. 
}); 

此代码的工作,只有当用户尝试编辑的东西,如果不是我可以关闭页面,这就是我想要的。

的问题是,该消息出现也当我提交表单

当我尝试clickbutton提交的消息也出现

本页面要求你确认你要离开 - 您输入的数据可能无法保存。

如何防止显示该消息,当我点击提交按钮??!

+0

保持一个** **标记为'alse',当你开始在设置页面编辑**标志**的值为'true'。然后在'beforeunload'中检查**标志**值并相应地设置消息。希望这会帮助你。 – Shiladitya

回答

0

保存在变量听众,并删除它提交

const onBeforeUnloadListener function(e){ 
 
    if($('#form').serialize()!=$('#form').data('serialize'))return true; 
 
    else e=null; 
 
    // i.e; if form state change show box not. 
 
} 
 

 
$(window).bind('beforeunload', onBeforeUnloadListener); 
 

 
$('#form').on('submit', function() { 
 
    $(window).unbind('beforeunload', onBeforeUnloadListener) 
 
}

+0

当我使用您的代码时,您的代码无法使用我可以关闭该页面并转到另一个页面,当我编辑表单中的某些内容时 –

相关问题