我已经找了一段时间,但是找不到解决方案。我想要以下内容:
- 在Bootstrap模态框中打开一个URL。当然,这个功能我已经实现了,所以内容会动态加载。
- 当用户在此模态框内点击按钮时,我希望当前模态框隐藏,并立即打开新的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
属性执行与此处相同的操作吗? - John Rdata-dismiss="modal"
。 - FDisk$(document).ready(function(){
$('#YOURMODAL')。on('shown.bs.modal',function(){
$('body')。addClass('modal-open');
});
});
- EvilSmurf没有看到具体的代码,很难给你一个精确的答案。但是,从 Bootstrap 文档中,您可以像这样隐藏模态框:
$('#myModal').modal('hide')
然后,在它隐藏后再显示另一个模态框:
$('#myModal').on('hidden.bs.modal', function () {
// Load up a new modal...
$('#myModalNew').modal('show')
})
你需要找到一种方式将URL推送到新的模态窗口中,但我认为这应该很简单。由于看不到代码,很难给出例子。
虽然不是完全相应,但当您想关闭当前模态框并打开一个新模态框时,它非常有用。
在同一个按钮的 HTML 中,您可以使用 data-dismiss 请求关闭当前模态框,并直接使用 data-target 打开一个新模态框:
<button class="btn btn-success"
data-toggle="modal"
data-target="#modalRegistration"
data-dismiss="modal">Register</button>
data-dismiss="modal"
的问题在于,当您关闭第二个模态框时,它会将您的内容向左移动。 - Kuldeep Dangidata-dismiss="modal"
的问题是它会使你的内容向左移动。
我分享一下对我有用的解决方法,问题陈述是从 pop2
打开 pop1
。
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;
});
$(function() {
return $(".modal").on("show.bs.modal", function() {
var curModal;
curModal = this;
$(".modal").each(function() {
if (this !== curModal) {
$(this).modal("hide");
}
});
});
});
如上所述,在同一个按钮的HTML中,您可以使用data-dismiss关闭当前模态窗口并直接使用data-target打开新模态窗口:
<button class="btn btn-success" data-toggle="modal" data-target="#modalRegistration" data-dismiss="modal">注册</button>
但这会导致滚动条消失,并且如果第二个模态窗口比第一个高,您会注意到这一点。
因此,解决方案是在模态窗口的内联样式中添加以下样式:
style="overflow-y:scroll;"
scrollbar-width: none; -ms-overflow-style: none; ::-webkit-scrollbar {display: none;}
- nxxn$("#buttonId").on("click", function(){
$("#currentModalId").modal("hide");
$("#currentModalId").on("hidden.bs.modal",function(){
$("#newModalId").modal("show");
});
});
data-dismiss="modal"
该功能用于关闭已打开的模型,您可以将其设置为新模型。
data-dismiss="modal" data-toggle="modal" id="forgotPassword" data-target="#ModalForgotPassword"
一篇详细的博客:http://sforsuresh.in/bootstrap-modal-window-close-current-open-new-modal/
您可以通过在同一个HTML元素中添加 data-dismiss="modal"
和 data-toggle="modal"
来在模态框之间切换。 您还需要将 data-target
属性设置为要打开的模态框。
以下是一个例子:
<button type="button" class="btn btn-primary" data-dismiss="modal" data-toggle="modal" data-target="#Modal2" >
Open second modal
</button>
记得为每个模态框设置一个id,并使data-target指向您想要打开的模态框。
与v4相同,只需使用属性data-bs-toggle
、data-bs-dismiss
和data-bs-target
像这样:
<button class="btn btn-primary" data-bs-dismiss="modal" data-bs-toggle="modal" data-bs-target="#Modal2" >
Open second modal
</button>
他们在v5文档中包含了一个示例来演示这个功能
注意:
为了使此功能正常工作,两个模态框需要彼此独立,而不是嵌套在彼此中。因此,如果您正在使用MVC框架(例如ASP.NET MVC)并将第一个模态框作为自己的局部视图,则不能在该视图中包含第二个模态框,您需要将第二个模态框放在父视图中。