jQuery 如何在jquery中关闭弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19087862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close popup in jquery
提问by commit
How to close opened popup when it opens based on condition.
根据条件打开时如何关闭打开的弹出窗口。
It looks very strange but I have requirement that based on some condition, if it becomes true popup should automatically close, How to do that?
看起来很奇怪,但我有一个要求,根据某些条件,如果它变成真正的弹出窗口应该自动关闭,怎么做?
I had return .dialog("close")
on document.ready
event but it is not working. Any idea?
我有回报.dialog("close")
的document.ready
事件,但它无法正常工作。任何的想法?
UPDATE
更新
I found problem is there actually popup is close but again after completing ready event it is opened again. Any help?
我发现问题是实际上弹出窗口已关闭,但在完成就绪事件后再次打开。有什么帮助吗?
回答by Ricardo Appleton
This is an old post, but incase someone else runs into this issue, I had a similar problem, with a jquery popup that wouldn't close even with
这是一篇旧帖子,但如果其他人遇到这个问题,我也遇到了类似的问题,即使使用 jquery 弹出窗口也不会关闭
$("#popup").dialog('close');
$("#popup").dialog('close');
The solution, adapted from a workmate's code, was to have a control variable on top of the script section and set it to 1 when I open the popup, and reset it to 0 when I want it to close. Additionally, I remove the popup from the html on pageLoad (still in javascript) like this:
根据同事的代码改编的解决方案是在脚本部分顶部有一个控制变量,并在我打开弹出窗口时将其设置为 1,并在我希望它关闭时将其重置为 0。此外,我从 pageLoad 上的 html 中删除弹出窗口(仍然在 javascript 中),如下所示:
<script>
<code>
var $l = jQuery.noConflict();
var cntDialog = 0;
</code>
function pageLoad(sender,args) {
$l(function () {
if (cntDialog == 0) {
RemoveDialog();
});
}
function initializeDialog(){
$l("#popup").dialog({
...
close: (function (sender,event) {
RemoveDialog();
})
});}
function RemoveDialog() {
cntDialog = 0;
$l("#popup").remove();
}
</script>
Hope it helps
希望能帮助到你
回答by Vibin Ashok
If it is a bootstrap modal, you can use the below code to show / hide the popup.
如果它是引导模式,您可以使用以下代码来显示/隐藏弹出窗口。
$('#modal').modal('toggle'); //To show and hide
if(condition) {
$('#modal').modal('hide'); //To hide
}
$('#modal').modal('show'); //To show
回答by Aivus
One of the simple solutions could be $('#dialog-close-btn').trigger('click') where you point to dialog close button.
一种简单的解决方案可能是 $('#dialog-close-btn').trigger('click') 指向对话框关闭按钮的位置。
回答by Bozidar Milivojevic
$('.my-modal').hide()
will close that active modal.
$('.my-modal').hide()
将关闭该活动模式。
回答by Naitik
You have to add some tag in server side script and you have to set event listener on client side for close the popup close.
您必须在服务器端脚本中添加一些标签,并且必须在客户端设置事件侦听器以关闭弹出窗口。
Means you have to set something like:
意味着你必须设置如下:
parent.postMessage("CLOSE","*");
on server.
在服务器上。
and set listener client side like:
并将侦听器客户端设置为:
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
// Listen to message from child window
// 监听子窗口的消息
eventer(messageEvent,function(e) {
var key = e.message ? "message" : "data";
var data = e[key];
},false);
I don't have time to check this. Hope this help you.
我没有时间检查这个。希望这对你有帮助。
Or i go through following URL:
或者我通过以下网址:
https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage
https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage
http://nyamsprod.com/blog/2012/introduction-to-window-postmessage/
http://nyamsprod.com/blog/2012/introduction-to-window-postmessage/
http://benalman.com/code/projects/jquery-postmessage/examples/iframe/
http://benalman.com/code/projects/jquery-postmessage/examples/iframe/
My given code is from URL:
我给定的代码来自 URL:
Html5 - Cross Browser Iframe postmessage - child to parent?
Html5 - 跨浏览器 Iframe postmessage - 子级到父级?
You can add dilogue close code inside listener.
您可以在侦听器中添加对话关闭代码。
Thanks
谢谢
回答by Beniamin Szabo
if (condition){
$('dialog id').dialog('close');
}
回答by Thierry J.
This should do the job in the popup window:
这应该在弹出窗口中完成工作:
<script>
$(function() {
if (condition) {
window.close();
}
});
</script>