我已经看了一会儿,但我找不到解决方案。我想要以下内容:
我已经盯着这几天了,希望有人可以帮助我。
提前致谢。
答案 0 :(得分:67)
我知道这是一个迟到的答案,但它可能有用。
正如上面提到的@karima所述,这是完成这项工作的正确而干净的方法。你实际上可以同时触发两个函数; trigger
和dismiss
模态。
HTML标记;
<!-- Button trigger modal -->
<ANY data-toggle="modal" data-target="TARGET">...</ANY>
<div class="modal fade" id="SELECTOR" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body"> ... </div>
<div class="modal-footer"> <!-- ↓ --> <!-- ↓ -->
<ANY data-toggle="modal" data-target="TARGET-2" data-dismiss="modal">...</ANY>
</div>
</div>
</div>
</div>
<强> Demo 强>
答案 1 :(得分:43)
如果没有看到具体的代码,很难给出准确的答案。但是,从Bootstrap文档中,您可以隐藏模态,如下所示:
$('#myModal').modal('hide')
然后,一旦它被隐藏,显示另一个模态:
$('#myModal').on('hidden.bs.modal', function () {
// Load up a new modal...
$('#myModalNew').modal('show')
})
你将不得不找到一种方法将URL推送到新的模态窗口,但我认为这是微不足道的。没有看到代码,很难给出一个例子。
答案 2 :(得分:30)
这不完全是响应,但是当你想要关闭当前模态并打开一个新模态时它会很有用。
在同一个按钮的html中,你可以要求用data-dismiss关闭当前模态并直接用data-target打开一个新的模态:
<button class="btn btn-success"
data-toggle="modal"
data-target="#modalRegistration"
data-dismiss="modal">Register</button>
答案 3 :(得分:9)
问题data-dismiss="modal"
是否会将您的内容转移到左侧
我正在分享对我有用的内容,问题陈述是从pop1
开启pop2
JS CODE
var showPopup2 = false;
$('#popup1').on('hidden.bs.modal', function () {
if (showPopup2) {
$('#popup2').modal('show');
showPopup2 = false;
}
});
$("#popup2").click(function() {
$('#popup1').modal('hide');
showPopup2 = true;
});
答案 4 :(得分:6)
我使用这种方法:
$(function() {
return $(".modal").on("show.bs.modal", function() {
var curModal;
curModal = this;
$(".modal").each(function() {
if (this !== curModal) {
$(this).modal("hide");
}
});
});
});
答案 5 :(得分:4)
如上所述,在同一按钮的html中,您可以要求使用data-dismiss关闭当前模式,并直接使用data-target打开新模式:
<button class="btn btn-success" data-toggle="modal" data-target="#modalRegistration" data-dismiss="modal">Register</button>
但这会导致滚动条消失,并且您会注意到,如果第二个模式的时间比第一个模式的时间长
所以解决方案是在模式内联样式中添加以下样式:
Style = "overflow-y : scroll ; "
答案 6 :(得分:4)
通过使用以下代码,您可以隐藏第一个模态并立即打开第二个模态,通过使用相同的策略,您可以隐藏第二个模态并显示第一个模态。
$("#buttonId").on("click", function(){
$("#currentModalId").modal("hide");
$("#currentModalId").on("hidden.bs.modal",function(){
$("#newModalId").modal("show");
});
});
答案 7 :(得分:3)
您需要将以下属性添加到要添加此功能的链接或按钮:
data-dismiss="modal" data-toggle="modal" id="forgotPassword" data-target="#ModalForgotPassword"
详细博客:http://sforsuresh.in/bootstrap-modal-window-close-current-open-new-modal/
答案 8 :(得分:2)
使用BootStrap 3,您可以尝试: -
var visible_modal = jQuery('.modal.in').attr('id'); // modalID or undefined
if (visible_modal) { // modal is active
jQuery('#' + visible_modal).modal('hide'); // close modal
}
经测试可以使用:http://getbootstrap.com/javascript/#modals(首先点击“启动演示模态”)。
答案 9 :(得分:2)
我有完全相同的问题,我的解决方案只有当模态对话框有[role =“dialog”]属性时才会出现:
/*
* Find and Close current "display:block" dialog,
* if another data-toggle=modal is clicked
*/
jQuery(document).on('click','[data-toggle*=modal]',function(){
jQuery('[role*=dialog]').each(function(){
switch(jQuery(this).css('display')){
case('block'):{jQuery('#'+jQuery(this).attr('id')).modal('hide'); break;}
}
});
});
答案 10 :(得分:1)
data-dismiss="modal"
它用于关闭现有打开的模型。您可以将其设置为新模型
答案 11 :(得分:1)
使用点击功能:
$('.btn-editUser').on('click', function(){
$('#viewUser').modal('hide'); // hides modal with id viewUser
$('#editUser').modal('show'); // display modal with id editUser
});
抬头:
确保您要显示的是独立模式。
答案 12 :(得分:1)
我和@ Gravity Grave有同样的问题,如果你使用
滚动不起作用data-toggle="modal" data-target="TARGET-2"
与
结合使用data-dismiss="modal"
滚动条无法正常工作并恢复滚动页面而不是模式。这是因为数据关闭从标记中删除了模态打开类。
我的解决方案最终是在模态上设置内部组件的html,并使用css淡入/淡出文本。
答案 13 :(得分:0)
此代码打开了关闭模式,打开了新模式,但立即关闭了新模式。
$(nameClose).modal('hide');
$(nameOpen).modal('show');
我在关闭其他后打开新模态的解决方案是:
function swapModals(nameClose,nameOpen) {
$(nameClose).modal('hide');
$(nameClose).on('hidden.bs.modal', function () {
console.log('Fired when hide event has finished!');
$(nameOpen).modal('show');
});
}
答案 14 :(得分:0)
隐藏模式对话框。
方法1:使用Bootstrap。
$('.close').click();
$("#MyModal .close").click();
$('#myModalAlert').modal('hide');
方法2:使用stopPropagation()
。
$("a.close").on("click", function(e) {
$("#modal").modal("hide");
e.stopPropagation();
});
在显示的方法调用之后,方法3:。
$('#myModal').on('shown', function () {
$('#myModal').modal('hide');
})
方法4:使用CSS。
this.display='block'; //set block CSS
this.display='none'; //set none CSS after close dialog
答案 15 :(得分:0)
我可能有点迟了,但是我想我已经找到了可行的解决方案。
必需-
jQuery
所有模态,带有关闭/关闭按钮,其属性设置如下-
<button type="button" class="btn close_modal" data-toggle="modal" data-target="#Modal">Close</button>
请参阅添加到按钮类中的类 close_modal
现在要关闭所有现有模态,我们将调用
$(".close_modal").trigger("click");
因此,无论您想在何处打开模态
只需添加上面的代码,您所有打开的模态都将关闭。
然后添加您的常规代码以打开所需的模式
$("#DesiredModal").modal();
答案 16 :(得分:0)
只需复制并粘贴此代码,您就可以尝试两种模式。关闭第一个模态后,打开第二个模态:
<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Open modal
</button>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- The Modal 2 -->
<div class="modal" id="myModal2">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Second modal</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$('#myModal').on('hidden.bs.modal', function () {
$('#myModal2').modal('show')
})
</script>
干杯!
答案 17 :(得分:0)
<div class="container">
<h1>Bootstrap modal: close current, open new</h1>
<p class="text-muted">
A proper and clean way to get this done without addtional Javascript/jQuery. The main purpose of this demo is was to answer this
<a href="http://stackoverflow.com/questions/18253972/bootstrap-modal-close-current-open-new">question on stackoverflow</a>
</p>
<hr />
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#demo-1">Launch Modal #1</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#demo-2">Launch Modal #2</button>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#demo-3">Launch Modal #3</button>
<!-- [ Modal #1 ] -->
<div class="modal fade" id="demo-1" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal"><i class="icon-xs-o-md"></i></button>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title caps"><strong>Demo Modal #1</strong></h4>
</div>
<div class="modal-body">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Input Placeholder..." />
<span class="input-group-btn"><button class="btn btn-default" type="button">Action</button></span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">×</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#demo-2" data-dismiss="modal">Close current, Launch Modal #2</button>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#demo-3" data-dismiss="modal">Close current, Launch Modal #3</button>
</div>
</div>
</div>
</div>
<!-- [ Modal #2 ] -->
<div class="modal fade" id="demo-2" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal"><i class="icon-xs-o-md"></i></button>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title caps"><strong>Demo Modal #2</strong></h4>
</div>
<div class="modal-body">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Input Placeholder..." />
<span class="input-group-btn"><button class="btn btn-default" type="button">Action</button></span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">×</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#demo-1" data-dismiss="modal">Close current, Launch Modal #1</button>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#demo-3" data-dismiss="modal">Close current, Launch Modal #3</button>
</div>
</div>
</div>
</div>
<!-- [ Modal #3 ] -->
<div class="modal fade" id="demo-3" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal"><i class="icon-xs-o-md"></i></button>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title caps"><strong>Demo Modal #3</strong></h4>
</div>
<div class="modal-body">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" placeholder="Input Placeholder..." />
<span class="input-group-btn"><button class="btn btn-default" type="button">Action</button></span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">×</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#demo-1" data-dismiss="modal">Close current, Launch Modal #1</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#demo-2" data-dismiss="modal">Close current, Launch Modal #2</button>
</div>
</div>
</div>
</div>
<hr />
<h3 class="caps">Usage:</h3>
<pre class="prettyprint"><!-- Button trigger modal -->
<ANY data-toggle="modal" data-target="TARGET">...</ANY>
<div class="modal fade" id="SELECTOR" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body"> ... </div>
<div class="modal-footer"> <!-- ↓ --> <!-- ↓ -->
<ANY data-toggle="modal" data-target="TARGET-2" data-dismiss="modal">...</ANY>
</div>
</div>
</div>
</div></pre>
</div> <!-- end .container -->
<hr />
<footer class="text-center"><a href="https://twitter.com/_elmahdim">@_elmahdim</a></footer>
<br />
<br />
答案 18 :(得分:0)
如果您使用mdb,请使用以下代码
var visible_modal = $('.modal.show').attr('id'); // modalID or undefined
if (visible_modal) { // modal is active
$('#' + visible_modal).modal('hide'); // close modal
}
答案 19 :(得分:0)
在打开一个新的模态之前关闭所有打开的模态
$('.modal').modal('hide'); // close all open modals
$('#login').modal('show'); // open a modal named #login
答案 20 :(得分:0)
尝试一下
$('#model1').modal('hide');
setTimeout(function(){
$('#modal2').modal('show');
},400);
答案 21 :(得分:0)
遇到同样的问题,请写一下这里,以防将来有人发现这个问题并且多个模式的问题也必须有滚动(I' m使用Bootstrap 3.3.7)
我所做的是在我的第一个模态中有一个这样的按钮:
<div id="FirstId" data-dismiss="modal" data-toggle="modal" data-target="#YourModalId_2">Open modal</div>
它会隐藏第一个,然后显示第二个,在第二个模态中我会有一个关闭按钮,如下所示:
<div id="SecondId" data-dismiss="modal" data-toggle="modal" data-target="#YourModalId_1">Close</div>
所以这将关闭第二个模态并打开第一个模式并使滚动工作我将此行添加到我的.css文件中:
.modal {
overflow: auto !important;
}
PS:只是旁注,你必须单独使用这些模态,当你隐藏第一个模态时,小模态不能嵌套在第一个模式中
所以这是基于bootstrap模式示例的完整示例:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal 1 -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Add lorem ipsum here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" data-toggle="modal" data-target="#exampleModal2">
Open second modal
</button>
</div>
</div>
</div>
</div>
<!-- Modal 2 -->
<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" data-toggle="modal" data-target="#exampleModal">Close</button>
</div>
</div>
</div>
</div>
答案 22 :(得分:0)
在第一个模式中:
使用下面的关闭链接替换页脚中的模态关闭链接。
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn btn-primary" data-toggle="modal" data-target="#second_model_id">Close</a>
</div>
在第二个模式中:
然后第二个模态用div标签替换顶部div。
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="add text for disabled people here" aria-hidden="true" id="second_model_id">
答案 23 :(得分:0)
BootStrap 3.x的简单而优雅的解决方案。可以通过这种方式重复使用相同的模态。
$("#myModal").modal("hide");
$('#myModal').on('hidden.bs.modal', function (e) {
$("#myModal").html(data);
$("#myModal").modal();
// do something more...
});
答案 24 :(得分:0)
所有答案都没有帮助我,因为我想要达到与问题中提到的完全相同的东西。
我为此创建了一个jQuery插件。
/*
* Raj: This file is responsible to display the modals in a stacked fashion. Example:
* 1. User displays modal A
* 2. User now wants to display modal B -> This will not work by default if a modal is already displayed
* 3. User dismisses modal B
* 4. Modal A should now be displayed automatically -> This does not happen all by itself
*
* Trying to solve problem for: http://stackoverflow.com/questions/18253972/bootstrap-modal-close-current-open-new
*
*/
var StackedModalNamespace = StackedModalNamespace || (function() {
var _modalObjectsStack = [];
return {
modalStack: function() {
return _modalObjectsStack;
},
currentTop: function() {
var topModal = null;
if (StackedModalNamespace.modalStack().length) {
topModal = StackedModalNamespace.modalStack()[StackedModalNamespace.modalStack().length-1];
}
return topModal;
}
};
}());
// http://stackoverflow.com/a/13992290/260665 difference between $.fn.extend and $.extend
jQuery.fn.extend({
// https://api.jquery.com/jquery.fn.extend/
showStackedModal: function() {
var topModal = StackedModalNamespace.currentTop();
StackedModalNamespace.modalStack().push(this);
this.off('hidden.bs.modal').on('hidden.bs.modal', function(){ // Subscription to the hide event
var currentTop = StackedModalNamespace.currentTop();
if ($(this).is(currentTop)) {
// 4. Unwinding - If user has dismissed the top most modal we need to remove it form the stack and display the now new top modal (which happens in point 3 below)
StackedModalNamespace.modalStack().pop();
}
var newTop = StackedModalNamespace.currentTop();
if (newTop) {
// 3. Display the new top modal (since the existing modal would have been hidden by point 2 now)
newTop.modal('show');
}
});
if (topModal) {
// 2. If some modal is displayed, lets hide it
topModal.modal('hide');
} else {
// 1. If no modal is displayed, just display the modal
this.modal('show');
}
},
});
工作小提琴以供参考, JSFiddle:https://jsfiddle.net/gumdal/67hzgp5c/
您只需使用我的新API调用&#34; showStackedModal()
&#34;而不只是&#34; modal('show')
&#34;。隐藏部分仍然可以与之前相同,并且显示&amp; amp;隐藏模态会自动处理。
答案 25 :(得分:0)
答案 26 :(得分:0)
我用另一种方式:
$('#showModal').on('hidden.bs.modal', function() {
$('#easyModal').on('shown.bs.modal', function() {
$('body').addClass('modal-open');
});
});
答案 27 :(得分:0)
$("#buttonid").click(function(){
$('#modal_id_you_want_to_hid').modal('hide')
});
// same as above button id
$("#buttonid").click(function(){
$('#Modal_id_You_Want_to_Show').modal({backdrop: 'static', keyboard: false})});