gpt4 book ai didi

JavaScript 多页表单 onbeforeunload 问题

转载 作者:行者123 更新时间:2023-11-28 08:24:24 26 4
gpt4 key购买 nike

我正在使用多页表单 jQuery 脚本构建一个测验系统,并且我希望能够在尝试关闭页面时警告用户。我使用下面的代码成功地做到了这一点:

$(document).ready(function($) {
window.onbeforeunload = function(){
return 'Sure you want to close the page?';
};
});

我的问题是,当我尝试提交表单并发布结果时,我收到警告,询问我是否要离开此页面。这是代码:

$('#quizForm').formwizard({
validationEnabled: true,
focusFirstInput : true,
formOptions: {
beforeSubmit: window.onbeforeunload = null,
resetForm: true
}
});

我做错了什么?

LE:我创建了这个 fiddle ,也许有人可以帮助我,我已经没有想法了。

http://jsfiddle.net/awLYY/5/

最佳答案

首先,您无需等待 DOM 准备好即可附加 onbeforeunload 处理程序。

第二,由于onbeforeunload是一个函数,因此您可以选择wither返回字符串,或者在向服务器提交一些数据时不返回任何内容

var isFormBeingSubmitted = false;

window.onbeforeunload = function() {
if( isFormBeingSubmitted ) {
return;
}
return 'Sure you want to close the page?';
};

// now before you submit your form, just enable the isFormBeingSubmitted
$('#quizForm').formwizard({
validationEnabled: true,
focusFirstInput : true,
formOptions: {
beforeSubmit: function() {
isFormBeingSubmitted = true;
return true;
},
resetForm: true
}
});

关于JavaScript 多页表单 onbeforeunload 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22591867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com