javascript - 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。请帮帮我
解决方案
推荐阅读
- pine-script - 修改用于股票交易的 Pine 脚本
- parsing - 需要将 monard chainrl 转换为 applicative 风格
- javascript - 当按钮未定义时,“未捕获的类型错误:无法设置未定义的属性‘onclick’”
- excel - 如何根据复选框值使组合框可见/不可见?
- javascript - 更改布局后删除了 application.js 函数
- reactjs - 为什么在下面的代码中会发生这个无限循环?反应和使用状态
- javascript - 我正在使用 momentjs,当使用 moment.startOf() 时,日期/时间会在一天结束时返回
- java - Java 使用 Shutting Yard 算法评估 RPN
- neo4j - 使用带有 apoc.periodic.commit 的参数给出 OutOfMemoryError
- python - PyQT 终端模拟器