不要询问用户是否提交表格 [英] Dont ask confirm if user submit the form
问题描述
我正在使用以下JavaScript代码警告用户,如果他试图在不提交表单的情况下重定向到另一个页面。
I am using following JavaScript code to warn a user if he tries to redirect to another page without submitting the form.
window.onbeforeunload = function() {
return 'Are you sure that you want to leave this page?';
};
这个工作正常。但我的问题是当用户尝试使用提交按钮确认框提交表单时将出现。
我不想询问用户是否提交表格,否则我想要确认。怎么可能?
This is working fine.But my problem is when user trying to submit the form using submit button confirm box will be appear. I don't want to ask the confirm if user submitting the form,otherwise I want to ask confirm. How is it possible?
推荐答案
维护状态变量。当用户点击提交按钮设置状态为 userSubmitted = True
;
状态变量可能包含全局变量或隐藏控件。
maintain a state variable. when user click submit button set state to userSubmitted=True
;
state variable may include a global variable or hidden control.
var userSubmitted=false;
$('form').submit(function() {
userSubmitted = true;
});
然后像这样检查
window.onbeforeunload = function() {
if(!userSubmitted)
return 'Are you sure that you want to leave this page?';
};
PS:对跨浏览器的onbeforunload兼容性进行交叉检查。
PS : cross check for onbeforunload compatibility for cross browser.
这篇关于不要询问用户是否提交表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!