如何禁用beforeunload行动时,用户提交表单? [英] How to disable beforeunload action when user is submitting a form?

查看:911
本文介绍了如何禁用beforeunload行动时,用户提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这个小片code:

<script>
$(window).bind('beforeunload', function() {
  $.ajax({
    async: false,
    type: 'POST',
    url: '/something'
    });
  });
</script>

我不知道,我怎么会禁用此请求时,用户点击提交按钮。

I wonder, how could I disable this request when user hits the submit button.

基本上像在这里,对SO。当你问一个问题,并决定关闭网页,你会得到一个警告窗口,但是当你提交的形式不会发生。

Basically something like here, on SO. When your asking a question and decide to close the page, you get a warning window, but that doesn't happen when you're submitting the form.

推荐答案

呼叫<一href="http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBMQFjAA&url=http%3A%2F%2Fapi.jquery.com%2Funbind%2F&ei=df89TZm1KIX6lweLv_Rg&usg=AFQjCNFbSCFR47fB4EUSCcAb2kOwYKuU0Q&sig2=btOaSEmQvj91dxupI95GeQ"><$c$c>unbind使用 beforeunload 事件处理程序:

$('form#someForm').submit(function() {
   $(window).unbind('beforeunload');
});

要prevent被提交表单,添加以下行:

To prevent the form from being submitted, add the following line:

   return false;

这篇关于如何禁用beforeunload行动时,用户提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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