Javascript 当用户提交表单时,如何禁用beforeunload操作?
我有一段代码:Javascript 当用户提交表单时,如何禁用beforeunload操作?,javascript,jquery,ajax,onbeforeunload,Javascript,Jquery,Ajax,Onbeforeunload,我有一段代码: <script> $(window).bind('beforeunload', function() { $.ajax({ async: false, type: 'POST', url: '/something' }); }); </script> 我想知道,当用户点击提交按钮时,我怎么能禁用此请求 基本上就像这里,等等。当您提出问题并决定关闭页面时,会出现警告窗口,但在提交表单时不会出现警告窗口。使用befor
<script>
$(window).bind('beforeunload', function() {
$.ajax({
async: false,
type: 'POST',
url: '/something'
});
});
</script>
我想知道,当用户点击提交按钮时,我怎么能禁用此请求
基本上就像这里,等等。当您提出问题并决定关闭页面时,会出现警告窗口,但在提交表单时不会出现警告窗口。使用beforeunload事件处理程序调用:
$('form#someForm').submit(function() {
$(window).unbind('beforeunload');
});
要防止提交表单,请添加以下行:
return false;
使用
确保你有这个之前,你主要提交功能!如果有这是我们使用的:
在DocumentReady上,我们调用beforeunload函数
$(document).ready(function(){
$(window).bind("beforeunload", function(){ return(false); });
});
在任何submit或location.reload之前,我们解除绑定变量
$(window).unbind('beforeunload');
formXXX.submit();
$(window).unbind("beforeunload");
location.reload(true);
正在寻找ASP.NET web应用程序的Detect OnForeUnload我是, 如果使用ASP.NET和母版页和内容页在页面上更改某些输入控件,我必须显示警告消息。我在母版页上使用了3个内容占位符,最后一个在表单之后
<form runat="server" id="myForm">
所以在表单结束标记之后和正文结束标记之前使用了这个脚本
<script>
var warnMessage = "Save your unsaved changes before leaving this page!";
$("input").change(function () {
window.onbeforeunload = function () {
return 'You have unsaved changes on this page!';
}
});
$("select").change(function () {
window.onbeforeunload = function () {
return 'You have unsaved changes on this page!';
}
});
$(function () {
$('button[type=submit]').click(function (e) {
window.onbeforeunload = null;
});
});
</script>
就绑定而言,beforeunload不能以这种方式可靠地工作。您应该以本机方式分配它
因此,我让它像这个绑定一样工作,而取消绑定对我来说也不起作用,在jQuery 1.7之后,事件API已经更新,.bind/.unbind仍然可以向后兼容,但是首选的方法是使用on/off函数
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.dirtyforms/2.0.0-beta00006/jquery.dirtyforms.min.js"></script>
<script type="text/javascript">
$(function() {
$('#form_verify').dirtyForms();
})
</script>
<title></title>
<body>
<form id="form_verify" action="a.php" method="POST">
Firt Name <input type="text">
Last Name <input type="file">
<input type="submit">
</form>
如果您正在使用bind,请使用以下命令:
$('form').submit(function () {
$(window).unbind('beforeunload');
});
这对所有表单提交都很好。超级旧问题,但可能对其他人有用 简单地从submit事件中分离beforeunload对我来说是不起作用的,因为即使表单中存在用户必须修复的错误,也会调用submit处理程序。因此,如果用户试图提交表单,然后收到错误,然后单击另一个页面,他们将能够在没有警告的情况下离开 这是我的变通方法,似乎效果不错
(function($) {
var attached = false,
allowed = false;
// catch any input field change events bubbling up from the form
$("form").on("change", function () {
// attach the listener once
if (!attached) {
$("body").on("click", function (e) {
// check that the click came from inside the form
// if it did - set flag to allow leaving the page
// otherwise - hit them with the warning
allowed = $(e.target).parents("form").length != 0;
});
window.addEventListener('beforeunload', function (event) {
// only allow if submit was called
if (!allowed) {
event.preventDefault();
event.returnValue = 'You have unsaved changes.';
}
});
}
attached = true;
});
}(jQuery));
这样,如果单击以离开表单内部的页面(如submit按钮),则不会显示警告。如果单击离开页面源于表单外部,则会警告用户。添加以下行:return false;,但我在哪里添加了这行?$window.unbind'beforeunload';向我抛出错误,如下所述未捕获的语法错误:意外的标记“.”但绑定在$window下也能正常工作。绑定“beforeunload”,函数{return“您确定要离开吗?”;};对我来说,这很管用,而雅各布·雷金的回答却不管用
(function($) {
var attached = false,
allowed = false;
// catch any input field change events bubbling up from the form
$("form").on("change", function () {
// attach the listener once
if (!attached) {
$("body").on("click", function (e) {
// check that the click came from inside the form
// if it did - set flag to allow leaving the page
// otherwise - hit them with the warning
allowed = $(e.target).parents("form").length != 0;
});
window.addEventListener('beforeunload', function (event) {
// only allow if submit was called
if (!allowed) {
event.preventDefault();
event.returnValue = 'You have unsaved changes.';
}
});
}
attached = true;
});
}(jQuery));