这是一个有5个步骤的表单向导,我不希望用户在任何步骤2-4中点击并丢失表单数据。我已经为提交函数添加了一个标志,并且需要为第一步添加这个标志。如果他们偶然到达那里并试图离开,我不希望弹出确认对话框。
<script type="text/javascript">
$(document).ready(function(){
var action_is_post = false;
$("form").submit(function () {
action_is_post = true;
});
//this is the trouble spot. on the first step the "navigation" of the form has a class
//of current (on step one = current)
$(this).ready(function () {
if ($("#stepDesc0").is(".current")) {
action_is_post = true;
}
);
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (!action_is_post)
return 'Using the browsers back, refresh or close button will cause you to lose all
form data. Please use the Next and Back buttons on the form.';
}
});
</script>发布于 2010-06-18 11:17:58
变量action_is_post被声明为$(document).ready()的局部变量,当它到达函数confirmExit()时,它在不同的作用域中。因此,它是未定义的,因此在条件语句中为false。尝试在$(document).ready()外部添加声明,如下所示:
var action_is_post = false;
$(document).ready(function(){
$("form").submit(function () {
action_is_post = true;
});这使变量成为全局变量,并且它的行为应该符合您的期望。
编辑:我检查了你页面中的代码,它非常复杂,很难理解。我的建议是简单地对confirmExit()函数中的类再次运行比较检查,而不是检查action_is_post变量的值。
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (!$("#stepDesc0").hasClass(".current"))
return 'Using the browsers back, refresh or close .....';
}https://stackoverflow.com/questions/3066918
复制相似问题