是否可以告诉 bootstrapNOT显示模式,通过返回false从内部到框架show.bs.modal
event?
EDIT:
使用 stopPropagation() 或简单地disable
按钮不是我想要的。我想处理逻辑inside show.bs.modal
事件。因为显示/隐藏模式是一个动态决定。
当然,可以在事件之外执行此逻辑并以编程方式调用$('#modal').show()
,如果这是唯一的方法。
2021 年 6 月 23 日 — 更新答案
当最初提出这个问题时(2021 年 5 月 13 日),无法阻止模态框在show.bs.modal
事件处理程序不止一次。
这是因为从 V4.0.0 到 V5.0.1 的所有 Bootstrap 版本都存在错误(V3 没有问题)。一个问题报告概述问题已提交并请求请求V4 and V5被创造。
在 Bootstrap V5.0.2 中,该问题已得到解决,如下面的代码片段所示。返回event.preventDefault()
(对于V5.0.2及以上版本)响应show.bs.modal
事件将阻止模态框的显示。不返回preventDefault()
响应后续事件将显示模式。
var modalCheckbox = document.getElementById('modalSwitch');
var modalCheckboxLabel = document.getElementById('modalLabel');
var modalPreventDefault = false;
var myModal = document.getElementById('myModal');
modalCheckbox.addEventListener('change', (event) => {
if (event.currentTarget.checked) {
modalPreventDefault = false;
modalCheckboxLabel.innerHTML = 'Modal Enabled';
} else {
modalPreventDefault = true;
modalCheckboxLabel.innerHTML = 'Modal Disabled';
}
});
myModal.addEventListener('show.bs.modal', (event) => {
console.log('Modal: ' + modalPreventDefault);
if (modalPreventDefault === true) {
return event.preventDefault();
}
});
li {
margin-bottom: 0.375rem;
}
code {
font-size: 1em;
}
.btn {
width: 11rem;
}
.form-switch .form-check-input {
cursor: pointer;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<div class="container mt-5">
<h2 class="text-center">Bootstrap <code>show.bs.modal</code> test case – V5.0.2</h2>
<div class="row justify-content-evenly">
<div class="col-6 col-md-4 col-lg-3">
<h3>Modal demo</h3>
<div class="form-check form-switch mb-3">
<input id="modalSwitch" class="form-check-input" type="checkbox" checked autocomplete="off">
<label id="modalLabel" class="form-check-label font-italic" for="modalSwitch">Modal Enabled</label>
</div>
<!-- Button trigger modal -->
<button id="modalButton" class="btn btn-primary" type="button" data-bs-toggle="modal" data-bs-target="#myModal">
Show Modal
</button>
</div>
</div>
</div>
<div class="container my-5">
<div class="row justify-content-center">
<div class="col-sm-11 col-md-9 col-lg-7">
<h3>Bootstrap Issue — Resolved</h3>
<p><a href="https://github.com/twbs/bootstrap/issues/34055" title="Put real link here!">Issue:</a> Modal event.preventDefault() for show.bs.modal: disables modal with fade class from being displayed again in V4 & V5 (up to V5.0.1).</p>
<p>The JavaScript for Bootstrap V4 & V5 (up to V5.0.1) stopped a modal from being displayed again, once displaying the modal has been prevented using <code>event.preventDefault()</code>.</p>
<p>The problem has been corrected in V5.0.2.</p>
<ul>
<li>A listener is attached to listen for the <code>show.bs.modal</code> event.</li>
<li>The checkbox switch setting allows or prevents the modal to be displayed:
<ul>
<li>If the checkbox is checked (Modal Enabled), the listener does nothing and the modal is displayed.</li>
<li>If the checkbox is un-checked (Modal Disabled), the listener returns <code>event.preventDefault()</code> and nothing is displayed.</li>
</ul>
</li>
</ul>
<p>In previous Bootstrap V5 versions (& V4), re-checking the checkbox (re-enabling) did not enable the modal to again be displayed after the modal was disabled. The <code>event.preventDefault()</code> now works as expected.</p>
</div>
</div>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 id="myModalLabel" class="modal-title">Modal title</h5>
<button type="button" class="close" data-bs-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-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
2022 年 6 月 9 日更新
Bootstrap V4.6.1于去年(2021年10月28日)发布。event.preventDefault()
for show.bs.modal
现在按预期工作。参考这个JSBin 演示查看它的运行情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)