在window.unload上提交表单时出现问题 [英] Problem submitting form on window.unload

查看:99
本文介绍了在window.unload上提交表单时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果表单信息已更新(album.updated = true),我在窗口卸载时提交表单时遇到问题:

I am having a problem submitting form on window unload if form information has been updated (album.updated = true):


window.onunload = function(evnt) {
  if (album.updated) {
    var save = confirm('Do you want to save changes?');
    if (save) {
      alert('submitting form...');
      document.edit_photos.submit();
    }
  }
}

它提醒提交表单但没有保存。但是,当我使用保存按钮进行常规提交时,它会保存信息。这可能有什么问题?

It alerts "submitting form" but nothing is saved. But when I do a regular submit with a Save button - it saves information. What could be wrong here?

推荐答案

您无法通过卸载 event。

You cannot reliably interact with the server from the unload event.

发送一个AJAX请求可能更可靠,但也不应该依赖。

Sending an AJAX request might be a little more reliable, but should also not be relied on.

相反,你应该处理 onbeforeunload 事件和返回你有未保存的更改;

这将指示浏览器显示一个允许用户取消导航的确认对话框。 (之后他可以手动保存)

Instead, you should handle the onbeforeunload event and return "You have unsaved changes";.
This will instruct the browser to show a confirmation dialog allowing the user to cancel the navigation. (After which he can save manually)

这篇关于在window.unload上提交表单时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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