bootstrap:从 show.bs.modal 事件中抑制模态

2023-12-12

是否可以告诉 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 &ndash; 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 &mdash; 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 (&amp; 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">&times;</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(使用前将#替换为@)

bootstrap:从 show.bs.modal 事件中抑制模态 的相关文章

  • 属性中的角度表达式

    我有一个使用 Angular 的页面 其中我从引导程序实现了弹出窗口 img class state msg 数据内容无法正确呈现 它按字面意思返回 item status message 而不是 message 的值 角度在 数据 属性中
  • 多选复选框下拉

    我正在使用多选复选框下拉菜单 请看例子jsfiddle http jsfiddle net manthan11 qqhczbvs 6 function lstStates multiselect 选择州后 它会显示 TEXT 值并用逗号连接
  • Bootstrap 3 导航栏不会在移动浏览器上折叠,但会在尺寸过小的桌面窗口中折叠

    我的带有最新版本 bootstrap 的 Rails 4 站点在移动设备中不会崩溃 但是如果我在桌面上打开该站点并缩小窗口 它就会崩溃 这是我的导航栏的代码 div class navbar navbar inverse navbar st
  • 如何使引导日期选择器只读?

    我正在努力创建嵌入式 内联日期选择器 它不可点击 它应该只显示日期 表现为只读 我正在做的是用模型中选定的日期填充日历 然后我尝试使其不可点击 这样用户就不会认为他可以编辑任何内容 我正在使用 eternicode bootstrap da
  • Twitter bootstrap:居中缩略图

    我有一个缩略图列表 它们有固定的尺寸 我希望连续缩略图的数量随着窗口的宽度而变化 使用 Twitter Bootstrap 这很容易 http jsfiddle net charlesbourasseau 5WvAL http jsfidd
  • 如何从 bootstrap-markdown.js 调用 .getContent 和 .parseContent

    我是使用 Bootstrap 插件的新手 刚刚通过代码学院 http www codecademy com en skills make an interactive website 我真的很想用这个很棒的引导 Markdown 插件 ht
  • 如何确定当前使用哪个网格选项

    我将 Bootstrap 3 用于使用 PHP 和 HTML 创建的网页 随着响应式网格和类的开启引导程序3您可以将多个类分配给一个 div 以根据当前屏幕尺寸定义不同的宽度 例如 div class col lg 3 col md 3 c
  • AngularJS - 关闭模态窗口

    我的内容包括 bootstrap css getbootstrap com 2 3 2 angular ui bootstrap tpls 0 10 0 min js from angular ui github io bootstrap
  • 如何在 Bootstrap 3 的导航栏中添加带有图标的搜索框?

    我正在使用新的 Twitter Bootstrap 3 并尝试放置一个像这样的搜索框 如下 在顶部导航栏中 在 Bootstrap 2 中 可以这样完成
  • 与 IE8 兼容的最新 jQuery 版本是什么?

    我正在开发 Bootstrap 3 网站 并且我使用的 jQuery 版本 1 9 1 遇到了一些问题 我升级到 2 1 0 我的问题消失了 但是 这破坏了 IE8 兼容性 因为 2 1 0 不再支持 IE8 并且 jQuery 在该浏览器
  • Bootstrap Affix Nav 导致下面的 Div 向上跳转

    我使用 Bootstrap 的 Affix 函数创建了一个 JSFiddle 以便在向下滚动并且标题移出视图时使导航保持在屏幕顶部 我遇到的问题是 当使用纯 HTML 时 导航下方的文本会过早地跳起来并隐藏在导航后面 查看有问题的代码her
  • 如何在bootstrap中默认隐藏侧边栏?

    我在这里有一个很好的参考 作为 Bootstrap 在设计 Web 表单应用程序时的侧边栏 http startbootstrap com template overviews simple sidebar http startbootst
  • 在 Bootstrap 中使用 CakePHP 时如何修改包装器 div 错误类

    我在用着Bootstrap 3 0RC1 with CakePHP 2 3 6 尝试利用那些漂亮的课程 例如has error and has warning for 验证状态 http getbootstrap com css forms
  • Bootstrap 3 RC2 自定义导航栏固定顶部走两行

    我正在使用 Bootstrap 3 RC2 我试图将导航栏固定在顶部 但不是全宽 我已经复制了 CSS 中的 navbar fixed top 声明 并创建了我的 在 IE 10 上 这效果很好 但在 Chrome 28 上 该栏分为两行
  • 通过纱线安装 bootstrap 的 Rails 找不到字体

    我有一个带 Bootstrap 的 Rails 5 应用程序 我用纱线安装了它 我做了以下事情 yarn add bootstrap bootstrap 3 3 7 version 3 3 7 resolved https registry
  • 为什么 Bootstrap 需要 jQuery? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我已经多次用谷歌搜索这个问题 但从未找到满意的答案 大多数答案似乎只是说 是的 Bootstrap 插件确实需要 jQuery https st
  • 删除下拉链接并在导航栏菜单中显示其所有项目

    我正在使用 Twitter Bootstrap 及其响应式设计来实现顶部典型的 Twitter Bootstrap 导航栏菜单 在那里我有一些链接和一个下拉菜单 当我将浏览器大小调整为768px或者更少 它会转变为一种新的导航菜单 这一切开
  • 关闭 bootstrap-select / select2 的自动对焦

    http www bootply com YAQrE52K6X http www bootply com YAQrE52K6X dataCombo selectpicker multiple true div class container
  • bootstrap navbar-static-top 菜单分成两行

    我遇到了引导导航栏的问题 我使用 navbar static top 制作了菜单 起初一切都很好 当菜单被展开并添加了一些项目时 现在随着折叠之前的宽度 它扩展到两行 看起来很糟糕 在 CSS bootstrap 中进行了更深入的挖掘 但没
  • 在网络上使用多种颜色的背景

    抱歉 如果标题有点误导 我想做的是用真正的浅灰色覆盖索引页面的背景 除了显示我的内容的部分 div class col sm 1 div div class col sm 8 div div class col sm 3 div 我希望 c

随机推荐

  • 列表的所有可能的子列表

    我需要编写一个函数来生成一个列表 其中包含列表中所有可能的子列表的列表 所以类型必须是 partitions a gt a 它应该给出 分区 1 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
  • 使用 JavaMail 的表

    我正在使用 JavaMail 发送电子邮件 我想将消息数据放入将嵌入电子邮件中的表中 接收消息的人将看到包含已填写数据的表格 我该怎么做呢 我猜你指的是 HTML 表 StringBuilder sb new StringBuilder s
  • 将未知数量的参数传递给 JavaScript 函数

    有没有办法传递未知数量的参数 例如 var print names function names foreach name in names console log name something like this print names
  • 是否可以使用 tqdm 进行 pandas 合并操作? [复制]

    这个问题在这里已经有答案了 我可以找到 tqdm 进度条用于 group by 和其他 pandas 操作的示例 但找不到有关合并或连接的任何内容 是否可以在 pandas 上使用 tqdm 进行合并 tqdm 支持 pandas 及其中的
  • 对 svg 过滤器的特征检测支持

    我需要检测浏览器是否支持 svg 过滤器 具体来说是 feGaussianBlur 我将如何对此进行测试 Safari 不支持过滤器 会默默地忽略过滤器 您可能还可以检查接口上可用的枚举 例如 var supportsfilter type
  • 设计 DAL 和 BLL - 相关表的单个/多个数据存储库

    在设计新的多层应用程序时 我很难为我的应用程序做出决定DAL and BLL层设计 假设我将员工信息分布在与主表具有 1 1 和 1 Many 关系的多个表中 下面列出了一些 员工 主表 员工 联系方式 详细信息 员工 教育 员工技能 员工
  • 如何绕过 www.instagram.com 的 429 错误?

    我今天向您求助是因为我对硒有疑问 我的目标是制作一个完全自动化的机器人 它可以创建一个包含解析详细信息 邮件 通行证 出生日期 的帐户 到目前为止 我已经几乎创建了该机器人 我只需要访问 Gmail 并获得确认 代码 我的问题就在这里 因为
  • pandas.DataFrame 和 numpy.array 中的 np.isreal 行为不同

    我有一个array像下面这样 np array hello world a 5 b 6 c 8 usa india d 9 e 10 f 11 and a pandas DataFrame像下面这样 df pd DataFrame A he
  • Java - 正则表达式在代码中查找注释

    一点fun这次用Java 我想编写一个从标准输入读取代码的程序 例如 逐行 例如 some comment class Main blah foo foo foo foo2 foo2 找到其中的所有评论并将其删除 我正在尝试使用正则表达式
  • 如何使用 Perl 列出具有特定名称模式的目录下的文件?

    我有一个目录 var spool在其中 名为的目录 a b c d e f g h i j k l m n o p q r s t u v x y z 在每个 字母目录 内 有一个名为 user 在这个里面 许多目录称为auser1 aus
  • String.split(".") 没有分割我的长字符串

    我正在执行以下操作 String test this is a example String test2 test split 问题 test2没有物品 但也有很多 in the test String 知道问题是什么吗 注意公共字符串 s
  • MvvmCross 无法在 iPhone 上为 EditingDidBegin 创建目标绑定

    我有一个绑定到 EditingDidBegin 的应用程序 它在 iPhone 模拟器 iOS 7 上运行良好 但在实际 iPhone 上运行时 我收到以下警告消息 MvxBind 警告 1 29 无法为 EditingDidBegin 创
  • 如何将一个 Jupyter 笔记本导入到另一个笔记本中

    显然有可能import将一个 Jupyter 笔记本插入另一个 链接页面有相当多的代码来完成此操作 我应该将该代码添加到导入笔记本中吗 页面对此并不清楚 它应该是一个通用的解决方案 因此将所有代码添加到导入其他笔记本的所有笔记本中是没有意义
  • 为什么我的列表项项目符号与浮动元素重叠

    我有一个 XHTML Strict 页面 我在其中将图像浮动在常规文本段落旁边 一切都很顺利 除非使用列表而不是段落 列表的项目符号与浮动图像重叠 更改列表或列表项的边距没有帮助 边距是从页面左侧开始计算的 但浮动会将列表项推到右侧insi
  • 微软认知服务情感 API。错误:“图像尺寸太小或太大。”

    我注意到认知服务套件中的情感 API 存在一个相当奇怪的错误 只要我发送 URL 一切都会正常工作 发送图像附件时 我收到此 JSON 错误 error code InvalidImageSize message Image size is
  • Sql 查询查找一系列相隔 5 分钟内发生的日期?

    这是 Sql Server 2008 我有一组数据 如下所示 Table UserAchievement id userId achievementId completedDate 当用户获得奖励时 奖励和用户以及日期都会被记录下来 我想要
  • Visual Studio 编译源文件的顺序是什么?

    我在 Visual Studio 2012 中有一个 C 库项目 包含这些文件 A h A cpp defines function Do A B h B cpp defines function Do B C h C cpp define
  • 如何在 CMake 文件中添加链接器或编译标志?

    我正在使用arm linux androideabi g 编译器 当我尝试编译一个简单的 Hello World 时程序编译得很好 当我通过在该代码中添加一个简单的异常处理来测试它时 它也可以工作 添加后 fexceptions 我猜它默认
  • 如何在 onSnapshot 之外从 firestore DB 获取数据

    当我尝试从 firestore 获取值并将其放入变量时 结果未定义 但在控制台中有效 My code this db collection Users doc uid get then docSnapshot gt if docSnapsh
  • bootstrap:从 show.bs.modal 事件中抑制模态

    是否可以告诉 bootstrapNOT显示模式 通过返回false从内部到框架show bs modal event EDIT 使用 stopPropagation 或简单地disable按钮不是我想要的 我想处理逻辑inside show