基于Bootstrap模态对话框只加载一次 remote 数据的解决方法

作者:jingxian 时间:2024-04-27 15:24:18 

摘要: 前端框架 Bootstrap 的模态对话框,可以使用 remote 选项指定一个 URL,这样对话框在第一次弹出的时候就会自动从这个地址加载数据到 .modal-body 中,但是它只会加载一次,不过通过在事件中调用 removeData() 方法可以解决这个问题。

1. Bootstrap 模态对话框和简单使用


<div id="myModal" class="modal hide fade">
 <div class="modal-header">
   <button type="button" class="close" data-dismiss="modal">x</button>
   <h3>对话框标题</h3>
 </div>
 <div class="modal-body">
   <p>对话框主体</p>
 </div>
 <div class="modal-footer">
   <a href="#" rel="external nofollow" rel="external nofollow" class="btn" data-dismiss="modal">取消</a>
   <a href="#" rel="external nofollow" rel="external nofollow" class="btn btn-primary" data-dismiss="modal">确定</a>
 </div>
</div>

显示效果与下图相似:

基于Bootstrap模态对话框只加载一次 remote 数据的解决方法

可以使用按钮或链接直接调用模态对话框,这是简单的用法:


<button type="button" data-toggle="modal" data-target="#myModal">打开对话框</button>
<a href="#myModal" rel="external nofollow" role="button" class="btn" data-toggle="modal">打开对话框</button>

这样只能把静态内容在对话框中显示出来,使用对话框的 remote 选项可以实现更强大的效果。

2. 使用 remote 选项让模态对话框加载页面到 .modal-body 中

有两种方法,一种是使用链接,另一种就是使用脚本。

2.1 使用链接


<a href="page.jsp" rel="external nofollow" data-toggle="modal" data-target="#myModal">打开对话框</a>

当点击此链接时,page.jsp 的内容会被加载到对话框的 .modal-body 中,随即显示对话框。

2.2 使用脚本


$("#myModal").modal({
 remote: "page.jsp"
});

这段脚本的效果和使用链接是一样的,当这段脚本执行后,page.jsp 的内容会被加载到对话框的 .modal-body 中,随即显示对话框。

这两种方法的背后,都是 Bootstrap 调用了 jQuery 的 load() 方法,从服务器端加载了 page.jsp 页面。但这个加载只会发生一次,后面不管你点击几次链接,或者执行几次脚本,哪怕改变传递给 remote 选项的值,对话框都不会重新加载页面,这真是个让人头疼的事情。不过问题还是能够解决的。

3. 移除数据,让对话框能够在每次打开时重新加载页面

在搜索并查阅了相关文档后,发现在对话框的 hidden 事件里写上一条语句就可以了:


$("#myModal").on("hidden", function() {
 $(this).removeData("modal");
});

也可以在每次打开对话框之前移除数据,效果是一样的。

注:上面的代码基于 Bootstrap v2,如果使用 Bootstrape v3,模态对话框的 HTML 和事件的写法有一些不同,例如对于上面的 hidden 事件,要写成:


$("#myModal").on("hidden.bs.modal", function() {
 $(this).removeData("bs.modal");
});
标签:模态框,remote,Bootstrap
0
投稿

猜你喜欢

  • mysql单字段多值分割和合并的处理方法

    2024-01-16 23:49:00
  • Python3+OpenCV2实现图像的几何变换(平移、镜像、缩放、旋转、仿射)

    2022-01-24 03:20:13
  • MYSQL事件查看器使用介绍

    2024-01-15 07:33:04
  • python3使用python-redis-lock解决并发计算问题

    2021-05-09 16:04:18
  • Bootstrap导航条学习使用(二)

    2024-05-02 17:31:16
  • Vue Router中应用中间件的方法

    2024-05-09 10:42:43
  • Python3 操作 MySQL 插入一条数据并返回主键 id的实例

    2024-01-21 06:05:16
  • 深入理解Tensorflow中的masking和padding

    2022-01-26 02:21:44
  • MySQL表设计优化与索引 (七)

    2010-10-25 20:06:00
  • WinHTTP Services 5.1 参考资料

    2010-03-27 20:49:00
  • Pyinstaller打包Pytorch框架所遇到的问题

    2023-02-17 06:26:58
  • 开源MySQL公司停止提供企业版源代码tar包

    2009-01-14 13:02:00
  • Keras搭建M2Det目标检测平台示例

    2023-11-10 10:49:01
  • python之如何查找多层嵌套字典的值

    2021-12-05 08:57:07
  • Python开发时报TypeError: ‘int‘ object is not iterable错误的解决方式

    2023-08-23 20:30:05
  • TensorFlow实现iris数据集线性回归

    2023-11-14 09:56:39
  • [Oracle] CPU/PSU补丁安装详细教程

    2024-01-27 22:17:52
  • python 中关于pycharm选择运行环境的问题

    2021-09-01 21:56:10
  • 简单谈谈GET和POST有什么区别

    2022-06-06 04:53:41
  • Django项目中用JS实现加载子页面并传值的方法

    2022-07-05 00:18:28
  • asp之家 网络编程 m.aspxhome.com