首页 > 解决方案 > jquery 表单提交在 Safari 和 Chrome 中不起作用

问题描述

我正在创建一个测试应用程序,向用户提出一些问题。在测验中间,如果用户选择离开测验,我会发出警告,说明所有更改都将丢失,并且将提交测试。我正在通过以下方式做到这一点

HTML

<form id="testsForm" method="POST" action="/tests/submit">
<!--test questions here-->
</form>

jQuery

var submitted = false;saveBeforeUnload = false;
window.onbeforeunload = function () {
          if (!submitted) {
             saveBeforeUnload=true;
             return "You are about to leave this order form. You will lose any information...";
          }
}//alert given to the user to confirm if he/she wishes to leave the form
setInterval(function() {
          if (saveBeforeUnload) {
               saveBeforeUnload = false;
               $("#testsForm").submit();
          }
}, 500);//form submit logic

此逻辑适用于 Firefox,但不适用于 Safari 和 Chrome。请帮帮我

标签: javascriptjqueryhtmlnode.js

解决方案


推荐阅读