javascript - hide.bs.modal 在 Bootstrap 3.2 w/Django 1.6.5 中未触发(远程模式内容)

标签 javascript jquery django twitter-bootstrap-3 modal-dialog

我正在开发一个显示主机列表的仪表板。当用户单击主机时,我想弹出一个模式对话框,其中包含有关该主机的其他详细信息。我目前正在使用 Bootstrap 的远程模式功能来触发 Django View 并填充模式。对于生成的第一个模态,此功能运行良好。之后,无论单击哪个链接,每次都会显示相同的模式。

我在 SO 和其他地方找到了有关此问题的帖子,这是建议的解决方案(使用 jQuery):

$('#hostDetail').on('hide.bs.modal', function(e) {
    $(this).removeData('bs.modal');
});

我尝试过这样做,但上面的代码从未触发。我尝试添加 console.log('hidden!') 只是为了确认,但日志从未显示任何内容。控制台中也没有显示任何错误。我是否忽略了一些简单的事情导致此代码无法工作?它开始让我发疯了!

这是我的 html,我在其中调用模态:

<a href="/portal/hostdetail/{{host.hostname}}" data-toggle="modal" host-name="{{host.hostname}}" data-target="#hostDetail">{{host.hostname}}</a>

这是模态本身(本地模态,而不是远程模态):

<div class="modal fade" id="hostDetail" tabindex="-1" role="dialog" aria-labelledby="remoteModalLabel" aria-hidden="true">  
    <div class="modal-dialog">  
        <div class="modal-content"></div>  
    </div>  
</div>  

此外,我一直在阅读 Bootstrap 中已弃用“远程”模式功能,并且我应该使用 jQuery 的 .load 函数。如果是这种情况,并且无法修复我当前的代码,有人可以将我链接到一个好的资源来准确展示如何通过 jQuery 完成这一切吗?对于 jQuery 和 Javascript,我是个菜鸟,所以一些非常基本的东西就太棒了!

提前致谢。

最佳答案

我最终使用 jQuery.load() 重做了所有事情。这里是所有相关的 html/javascript。我确信代码并不那么干净,因为我是菜鸟,但它有效。

基本页面 HTML:

<td><a href="/portal/hostdetail/{{host.hostname}}" current_host="{{host.hostname}}" data-target="#hostDetail">{{host.hostname|upper}}</a></td>

<div class="modal " id="hostDetail" tabindex="-1" role="dialog" aria-labelledby="remoteModalLabel" aria-hidden="true">  
    <div class="modal-dialog">  
        <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
              </div>
              <div class="modal-body">

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
              </div>

        </div>  
    </div>  
</div>      

基页 JavaScript:

$('a[data-target="#hostDetail"]').click(function(event) {   
    event.preventDefault();
    $("#hostDetail").modal('hide');
    var current_host = $(this).attr("current_host");
    var myModal = $('#hostDetail');
    var modalBody = myModal.find('.modal-content');
    // load content into modal
    var remote = '/portal/hostdetail/' + current_host;
    modalBody.load(remote, null, function (data) {
        // display modal
        myModal.modal('show');  
        });

});

远程页面(模态)html:

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Remote file for Bootstrap Modal</title>  
</head>
<body>

    <div class="modal-header">
        Header text goes here
    </div>

    <div class="modal-body">
        Body text goes here
    </div>

    <div class="modal-footer">
        Footer stuff goes here
    </div>

</body>
</html>

关于javascript - hide.bs.modal 在 Bootstrap 3.2 w/Django 1.6.5 中未触发(远程模式内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26640872/

相关文章:

javascript - jstree:创建新节点不起作用

javascript - 如果我的输入元素位于标签内,jQuery 验证是否有效?

python - Django - TabularInline 没有添加按钮

python - 如何集成两个不同数据库的Django项目?

javascript - 无法编译;使用 grunt 响应图像找不到有效的源文件

javascript - 在 ChartJS 的工具提示中计算值

javascript - 附加到回调函数时变量未定义(Javascript/Dojo)

javascript - jQuery/Javascript 创建一个文本文件供用户下载

jquery - 将div元素传递给成功回调jquery

python - 减少 django modelform 中的重复查询