我已经寻找了一段时间,但是找不到解决方案。我想要以下内容:
- 在Bootstrap模式下打开一个URL。我有这个问题。因此,内容是动态加载的。
- 当用户在此模式中按下按钮时,我希望隐藏当前模式,然后在此之后,立即希望使用新URL(用户单击)打开一个新模式。第二模态的该内容也被动态加载。
- 如果用户随后关闭了第二个模态,则第一个模态必须再次返回。
我已经盯着这几天了,希望有人能帮助我。
提前致谢。
我已经寻找了一段时间,但是找不到解决方案。我想要以下内容:
我已经盯着这几天了,希望有人能帮助我。
提前致谢。
Answers:
没有看到特定的代码,很难给您一个准确的答案。但是,从Bootstrap文档中,您可以像这样隐藏模式:
$('#myModal').modal('hide')
然后,隐藏另一个模态:
$('#myModal').on('hidden.bs.modal', function () {
// Load up a new modal...
$('#myModalNew').modal('show')
})
您将必须找到一种方法来将URL推送到新的模式窗口,但是我认为这是微不足道的。如果不看代码,很难给出一个例子。
我知道这是一个较晚的答案,但这可能很有用。正如上面@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>
templateUrl
和windowClass
属性来执行与此处相同的操作?
data-dismiss="modal"
了按钮
-webkit-backface-visibility: hidden;
对HTML,CSS体
它不完全是响应,但是在您要关闭当前模态并打开新模态时很有用。
在同一按钮的html中,您可以要求使用data-dismiss关闭当前模式,并直接使用data-target打开新模式:
<button class="btn btn-success"
data-toggle="modal"
data-target="#modalRegistration"
data-dismiss="modal">Register</button>
data-dismiss="modal"
是,当您关闭第二个模式时,它将使您的内容向左移动。
问题data-dismiss="modal"
是它会将您的内容向左移动
我正在分享对我有用的东西,问题陈述pop1
从pop2
JS代码
var showPopup2 = false;
$('#popup1').on('hidden.bs.modal', function () {
if (showPopup2) {
$('#popup2').modal('show');
showPopup2 = false;
}
});
$("#popup2").click(function() {
$('#popup1').modal('hide');
showPopup2 = true;
});
通过使用以下代码,您可以隐藏第一个模态并立即打开第二个模态,通过使用相同的策略,您可以隐藏第二个模态并显示第一个模态。
$("#buttonId").on("click", function(){
$("#currentModalId").modal("hide");
$("#currentModalId").on("hidden.bs.modal",function(){
$("#newModalId").modal("show");
});
});
您需要在要添加此功能的链接或按钮上添加以下属性:
data-dismiss="modal" data-toggle="modal" id="forgotPassword" data-target="#ModalForgotPassword"
一个detaile博客:http ://sforsuresh.in/bootstrap-modal-window-close-current-open-new-modal/
data-dismiss="modal"
它用于关闭现有打开的模型。您可以将其设置为新模型
如上所述,在同一按钮的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 ; "
使用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(首先单击“启动演示模态”)。
.attr
一点,对吗?这项工作会不会很好?var visible_modal = jQuery('.modal.in'); if (visible_modal) { visible_modal.modal('hide'); }
。
我有完全相同的问题,并且只有模态对话框具有[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;}
}
});
});
在第一个模式中:
用下面的关闭链接替换页脚中的模式关闭链接。
<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标签替换top div。
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="add text for disabled people here" aria-hidden="true" id="second_model_id">
遇到相同的问题,在此处编写此代码,以防将来有人偶然发现此问题并遇到必须滚动的多个模式问题(我正在使用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:请注意,您必须分别具有这些模态,次要模态不能嵌套在第一个模态中,因为您隐藏了第一个模态
因此,这是基于引导程序模式示例的完整示例:
<!-- 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>
试试这个
$('#model1').modal('hide');
setTimeout(function(){
$('#modal2').modal('show');
},400);
$("#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})});
我使用另一种方式:
$('#showModal').on('hidden.bs.modal', function() {
$('#easyModal').on('shown.bs.modal', function() {
$('body').addClass('modal-open');
});
});
如果要在打开新模态的同时关闭以前打开的模态,则必须先关闭当前打开的模态,然后再给出400ms的超时时间使其关闭,然后再打开新模态,如下面的代码所示,从javascript / jquery中执行此操作:
$('#currentModal').modal('hide');
setTimeout(function() {
//your code to be executed after 200 msecond
$('#newModal').modal({
backdrop: 'static'//to disable click close
})
}, 400);//delay in miliseconds##1000=1second
如果您尝试使用,data-dismiss="modal"
则它将出现滚动问题,如@gravity和@kuldeep在注释中所述。
没有答案对我有帮助,因为我想实现与问题中提到的完全相同的东西。
我为此创建了一个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“ showStackedModal()
”而不是“ ”进行调用modal('show')
。隐藏部分仍可以与以前相同,并且自动处理显示和隐藏模态的堆叠方法。
BootStrap 3.x的简单而优雅的解决方案。可以以这种方式重复使用相同的模式。
$("#myModal").modal("hide");
$('#myModal').on('hidden.bs.modal', function (e) {
$("#myModal").html(data);
$("#myModal").modal();
// do something more...
});
<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 />
我可能有点迟了,但是我认为我已经找到了可行的解决方案。
必填-
jQuery的
所有带有关闭/解除按钮的模态,其属性设置如下:
<button type="button" class="btn close_modal" data-toggle="modal" data-target="#Modal">Close</button>
请查看添加到按钮类中的类close_modal
现在关闭所有现有的模态,我们将调用
$(".close_modal").trigger("click");
因此,无论您要打开模式
只需添加上面的代码,您所有打开的模态将被关闭。
然后添加您的常规代码以打开所需的模式
$("#DesiredModal").modal();
隐藏模态对话框。
方法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
此代码打开了关闭模式,打开了新模式,但立即关闭了新模式。
$(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');
});
}
只需复制并粘贴此代码,您就可以尝试两种模式。关闭第一个模态后,打开第二个模态:
<!-- 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>
干杯!