相关疑难解决方法(0)

如何覆盖OnBeforeUnload对话框并将其替换为我自己的?

我需要在用户离开页面之前警告用户未保存的更改(这是一个非常常见的问题).

window.onbeforeunload=handler
Run Code Online (Sandbox Code Playgroud)

这有效,但它会引发一个默认对话框,其中包含一条包含我自己文本的令人生气的标准消息.我需要完全替换标准消息,所以我的文本是清楚的,或者(更好)用一个模式对话框替换整个对话框使用jQuery.

到目前为止,我失败了,我还没有找到任何似乎有答案的人.它甚至可能吗?

我的页面中的Javascript:

<script type="text/javascript">   
   window.onbeforeunload=closeIt;
</script>
Run Code Online (Sandbox Code Playgroud)

closeIt()函数:

function closeIt()
{
  if (changes == "true" || files == "true")
  {
      return "Here you can append a custom message to the default dialog.";
  }
}
Run Code Online (Sandbox Code Playgroud)

使用jQuery和jqModal我尝试过这种事情(使用自定义确认对话框):

$(window).beforeunload(function() {
        confirm('new message: ' + this.href + ' !', this.href);
        return false;
    });
Run Code Online (Sandbox Code Playgroud)

这也行不通 - 我似乎无法绑定到beforeunload事件.

javascript jquery onbeforeunload

341
推荐指数
6
解决办法
34万
查看次数

jquery在没有单击保存按钮的情况下离开页面时发出警告

如果您更改页面上的数据然后离开页面,以下代码将执行警告.我想要 - 除非用户按下保存按钮.

这是jquery代码:

$(document).ready(function() {
  formmodified=0;
    $('form *').change(function(){
        formmodified=1;
    });
  window.onbeforeunload = confirmExit;
    function confirmExit() {
      if (formmodified == 1) {
          return "New information not saved. Do you wish to leave the page?";
      }
  }
});
Run Code Online (Sandbox Code Playgroud)

这是保存数据的按钮:

<input class="btn-primary" name="commit" type="submit" value="Save Material">
Run Code Online (Sandbox Code Playgroud)

更新谢谢tymeJV !!!!!!!

如果其他人需要它,这有效:

$(document).ready(function() {
    formmodified=0;
    $('form *').change(function(){
        formmodified=1;
    });
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        if (formmodified == 1) {
            return "New information not saved. Do you wish to leave the page?";
        }
    }
    $("input[name='commit']").click(function() …
Run Code Online (Sandbox Code Playgroud)

jquery

39
推荐指数
3
解决办法
4万
查看次数

在 beforeunload/unload 上发送 Ajax 调用

场景:我正在创建一个事件注册模块,用户可以在其中从日历中选择一个日期进行预订,填写一些输入字段并支付预订日期。其中一项要求是,每个日期只能进行一次预订。这出现了两个人想要在同一时间预订相同日期的情况。因此,我们需要向稍后来预订该日期的用户显示一条消息,表明该特定日期的预订已经在进行中。请稍后再回来查看

为此,每当用户选择日期时,我都会在我的数据库中创建一个临时条目。针对该条目,我可以向后来来的其他用户显示该消息。

从逻辑上讲,如果第一个用户,则必须删除该条目:

  1. 选择其他日期。
  2. 关闭他的浏览器或离开页面

以便它可供想要预订同一日期的第二个用户使用。

问题:一旦用户更改了他选择的日期,我就可以删除该条目。我无法实现第二个。我编写了一个函数,该函数将删除该行并在 onbeforeunload jQuery 事件上调用该函数。显然,该函数不适用于 jQuery。

window.onbeforeunload = function(){
  deleteTempEntry(); //This sends an ajax call to delete the entry. Not working.

  alert("The second alert"); // even this alert doesn't work.

  // I actually intend to use confirm box to make sure to not to delete the entry if the user does not leave the page.
  confirm("Your current progress will be removed. Are you sure?"); 

  //Calling this return function shows the warning …
Run Code Online (Sandbox Code Playgroud)

javascript php ajax jquery date

3
推荐指数
1
解决办法
8434
查看次数

标签 统计

jquery ×3

javascript ×2

ajax ×1

date ×1

onbeforeunload ×1

php ×1