如何检测JQuery Mobile对话框已关闭
How to detect that JQuery-Mobile dialog was closed
我有一个jQuery Mobile对话框,我从页面main.html
打开,如下所示:
$.mobile.changePage("settings.html", {
role: "dialog",
transiation: "pop",
changeHash: true
});
现在,当对话框关闭时,我想在我的网站main.html
上获得一个回调,这样我就可以继续处理对话框中输入的信息。
更新:提到我应该在我的初始位置main.html
上收到回调
解决方案
Dialog只是经典jQueryMobile页面的另一个版本,因此也可以使用页面事件。
您要使用的是隐藏事件之前的页面。
工作示例:http://jsfiddle.net/Gajotres/cbc5q/
$(document).on('pagebeforehide', '#second', function(){
alert('Close Dialog');
});
您可以使用的其他事件很少,您可以在文章中找到它们,只需查找一章即可:页面事件转换顺序
证明
因为你使用了几个HTML页面,我给你做了一个由两个HTML文件组成的工作示例:
index.html
<!DOCTYPE html>
<html>
<head>
<title>jQM Complex Demo</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script>
$(document).on('pagebeforeshow', '#index',function (e, data) {
var prevPage = data.prevPage.attr('id');
if(prevPage === 'second'){
alert('sdfs');
}
});
</script>
</head>
<body>
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
</div>
<div data-role="content">
<a href="dialog.html" data-role="button">Open dialog</a>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
</body>
</html>
dialog.html
<!DOCTYPE html>
<html>
<head>
<title>jQM Complex Demo</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
</head>
<body>
<div data-role="dialog" id="second">
<div data-theme="a" data-role="header">
<h3>
Second Page
</h3>
</div>
<div data-role="content">
<a href="index.html" data-role="button">close Dialog</a>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
</body>
</html>
首先请注意,在dialog.html中,javascript被放置在页面/对话框div中。这是因为jQuery Mobile在打开其他页面时会剥离HEAD内容+其他所有内容。这就是javascript被放置在页面/对话框分区中的原因。
这应该做到:
$(document).on('pagebeforeshow', '#index',function (e, data) {
var prevPage = data.prevPage.attr('id');
if(prevPage === 'second'){
alert('sdfs');
}
});
这段代码将在初始页面上触发,就在它显示之前。如果上一页是一个对话框,则执行一个操作。
我想出了一个奇特的方法来实现我想要的:
$(document).on( "dialogcreate", function( event, ui ) {
$(document).one('pagechange', function (e, f) {
$(document).one('pagechange', function (e, f) {
console.log('Settings closed');
});
});
});
说明:
- 创建对话框时,会引发
dialogcreate
事件 - 然后,我注册一个
pagechange
事件,当jquery mobile打开对话框时会抛出该事件。所以现在对话框打开了 - 现在,我注册了另一个单独的
pagechange
事件,当对话框关闭并且我回到原始位置时,该事件将被抛出
请注意,如果设置对话框在最终被取消之前触发了另一个pagechange
事件,那么这将不起作用。然而,这对我来说是有效的。
我希望有一个更干净的方法。我想知道为什么没有dialogclose
这样的事件。
相关文章:
- jquery mobile和javascript对话框未显示
- Button onClick事件在jQuery Mobile对话框中触发过多次
- jQuery Mobile//主题化我的动态对话框
- jQuery Mobile 对话框不会显示在 AJAX 请求上
- JQuery Mobile - 关闭对话框后加载错误的页面,并且Javascript并不总是有效
- jQuery Mobile对话框立即关闭
- jQuery Mobile对话框空白
- JQuery Mobile-与对话框交互
- 当我使用jquery mobile从一个页面移动到另一个页面时,如何显示进度对话框
- 如何检测JQuery Mobile对话框已关闭
- 列表视图点击事件只在jQuery Mobile中的对话框中第一次发生
- Jquery Mobile-关闭对话框(firefox)后跳过固定的标头
- jQuery Mobile:以编程方式打开一个对话框后关闭另一个
- 无法在JQuery Mobile中以编程方式显示对话框
- jquery-mobile:不能改变对话框中的输入值
- Jquery Mobile,不工作的对话框
- jquery Mobile弹出对话框可在选项上取消'不起作用
- jQuery Mobile:如何使对话框打开多次
- jQuery Mobile和普通jQuery对话框冲突/不工作
- 如何在jQuery Mobile中显示对话框