我正在使用此jquery代码来警告用户在完成我们的在线应用程序之前导航。我们有很多用户在菜单/列表字段中点击后退按钮(不知道它是导航快捷方式)并丢失他们在应用程序中输入的所有数据。问题是,我无法弄清楚当他们点击提交按钮时如何防止显示警告。
我看过这个网站上的其他几个类似的帖子,但我也无法让他们工作。实际上对于大多数人来说,我甚至无法让弹出工作。我对jquery不是很熟悉,所以我可能很简单。 (我实际上有另一个解决方案,我在这里工作:http://www.citizensmemorial.com/careers/online-app.html
<script> function setConfirmUnload(on) {
window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
return 'You have started filling out this application.' +
' If you navigate away from this page without' +
' first saving your data, the changes will be' +
' lost.';
}</script>
<script>
$(document).ready(function() {
$(':input',document.form1).bind(
"change", function() {
setConfirmUnload(true);
}
); // Prevent accidental navigation away
});
</script>
答案 0 :(得分:0)
我已撤消您的验证,因此您在设置确认时无需解析任何内容{.1}}。
setConfirmUnload
我已将function setConfirmUnload(disable) {
window.onbeforeunload = disable ? null : unloadMessage;
}
function unloadMessage() {
return 'You have started filling out this application.' +
' If you navigate away from this page without' +
' first saving your data, the changes will be' +
' lost.';
}
setConfirmUnload();
jQuery(function($) { // <-- Dom is ready (same as $(document).ready(function($) {)
$("form").on("submit", setConfirmUnload);
});
绑定到表单提交处理程序。调用中的第一个参数是setConfirmUnload
这将在测试时验证为真。
You can try a simple demo here
如果您想在输入任何内容时首先激活卸载处理程序,您可以使用:
jQueryEvent Object