javascript - 如果用户在页面上输入内容但未保存则显示消息

标签 javascript jquery

如果用户输入了一些内容但未保存并尝试离开页面,我如何显示类似弹出模式的消息?

类似于:

enter image description here

如果您尝试转到 Facebook 中的另一个页面,但如果您输入 URL,则会出现 native 警告框,例如:

enter image description here

1.) 有什么区别?警报是内置于浏览器中的东西吗?

2.) 当类似事件发生时,我如何使用 jQuery 创建我自己的自定义模式框?

谢谢

最佳答案

试试这个

window.onbeforeunload = function(evt){

    //Show you custom confirm dialog box here
    //Inside the confirm dialog box if the use say YES leave the page
    //Then you should set `window.onbeforeunload` to null and
    //redirect to required location so that it will not ask again.
    var dialog = $('#confirmDialog').dialog('open');

    //This will prevent the default dialog box of the browser.
    return false;
}

参见 MSDN article on onbeforeunload

关于javascript - 如果用户在页面上输入内容但未保存则显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7325396/

相关文章:

javascript - 按比例调整不断增长的 div 的大小?

javascript - 将变量从一个 JavaScript 回调匿名函数传递到另一个

jQuery 从另一个更改操作更改操作

javascript - jquery 删除变量内的 html 元素(jquery 对象)

javascript - 如何在 jQuery 中获取文本框的更新值

javascript - 如何根据数据库的输出显示输入字段

javascript - Google 搜索引擎结果出现在正常产品搜索中

javascript - 递归更改 JSON 键名(全部大写)?

javascript - 用户从下拉列表中构建过滤器 ('selection' )

javascript - 将 MySQL 查询结果传递给 Django 中的 Javascript/Jquery