导航警报而不保存修改 [英] navigate away alert without saving modifications

查看:104
本文介绍了导航警报而不保存修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP开发一个网站。

I'm developing a website in php.

当用户尝试编辑或在表单中添加内容时,我想显示类似javascript警报的消息并尝试导航到其他部分而不保存修改,我想向他们显示一条消息,

I want to show a message something like javascript alert, when a user tries to edit or add something in a form and tries to navigate to some other section without saving the modification, i want to show a message to them,

您将要从此页面导航,您的修改没有保存,你想继续吗?

that you are about to navigate about from this page, your modifications are not saved, do you want to continue?

我该怎么办?

任何人都有一个想法???请与我分享..

any one have an idea ???please share it with me..

谢谢

推荐答案

使用 beforeunload 活动。伪代码:

window.onbeforeunload = function(e){
   e = e || window.event;
   // check if the user has edited sth
   if(userHasEditedSomething()){
      var msg = "You have unsaved changed. Do you want to navigate away from this page?";
      e.returnValue = msg;
      return msg;
   }
}

你的工作是实现将返回的userHasEditedSomething函数当用户有未保存的更改(填写的表单字段)时为true,否则为false。

Your job is to implement the userHasEditedSomething function that will return true when the user has unsaved changes (filled-in form fields) and false otherwise.

大多数浏览器都支持此事件

most of the browsers support this event

这篇关于导航警报而不保存修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆