jQuery 打开新模式时如何隐藏 Bootstrap 上一个模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12531653/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:47:57  来源:igfitidea点击:

How to hide Bootstrap previous modal when you opening new one?

jquerytwitter-bootstrapmodal-dialog

提问by MID

I have such trouble: I have authentification which is made using Bootstrap modals.

我有这样的麻烦:我有使用 Bootstrap 模态进行的身份验证。

When user opens sign in modal he can go to sign up modal ( or other ) . So, I need to close previous one.

当用户打开登录模式时,他可以去注册模式(或其他)。所以,我需要关闭上一个。

Now I'm closing them like this:

现在我像这样关闭它们:

$(document).ready(function(){
  $("a").click(function() {
  $("#login").hide();
  $("#sign_up").hide();
 })  
});

but when I'm doing this - I can't reopen sign in modal( after click on sign up from sign in) until I refresh the page.

但是当我这样做时 - 在我刷新页面之前,我无法重新打开登录模式(点击登录后)。

Can someone suggest how to check on what modal I'm clicking on the link and close it, when call another one ? Is there any universal way to close previous modal ?

有人可以建议如何检查我点击链接的模式并在调用另一个模式时关闭它吗?有什么通用的方法可以关闭以前的模态吗?

回答by willglynn

You hide Bootstrap modals with:

您可以使用以下命令隐藏 Bootstrap 模态:

$('#modal').modal('hide');

Saying $().hide()makes the matched element invisible, but as far as the modal-related code is concerned, it's still there. See the Methods section in the Modals documentation.

话说$().hide()让匹配的元素不可见,但就模态相关的代码而言,它仍然存在。请参阅Modals 文档中的方法部分。

回答by Vikash Kumar

Toggle both modals

切换两种模式

$('#modalOne').modal('toggle');
$('#modalTwo').modal('toggle');

回答by TolMera

The best that I've been able to do is

我能做的最好的是

$(this).closest('.modal').modal('toggle');

This gets the modal holding the DOM object you triggered the event on (guessing you're clicking a button). Gets the closest parent '.modal' and toggles it. Obviously only works because it's inside the modal you clicked.

这将获取包含您触发事件的 DOM 对象的模态(猜测您正在单击一个按钮)。获取最近的父 '.modal' 并切换它。显然只有效,因为它在您单击的模态内。

You can however do this:

但是,您可以这样做:

$(".modal:visible").modal('toggle');

This gets the modal that is displaying (since you can only have one open at a time), and triggers the 'toggle' This would not work without ":visible"

这将获取正在显示的模式(因为您一次只能打开一个),并触发“切换”如果没有“:visible”,这将无法工作