jquery - 设置模态标题

标签 jquery bootstrap-modal

我有我的模态:

<div id="defaultModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal-title" 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>
                <h3 class="modal-title" id="modal-title">Modal Title</h3>
            </div>
            <div class="modal-body" data-table="table" data-id="">
            </div>
        </div>
    </div>
</div>

我有我的按钮:

<button  href="page.php" data-title="New Data" class='call-modal btn btn-success' > 
    <i class="glyphicon glyphicon-plus"></i>
</button>   

单击按钮后,我使用“call-modal”类:

$('.call-modal').on('click', function(e){
    e.preventDefault();
    $('#defaultModal')
    .find('.modal-header > h3').text("Teste").end()            
    .find('.modal-body').load($(this).attr('href')).end()            
    .modal('show');
});

我可以加载内容,但我想使用数据标题设置标题。

知道为什么它不起作用吗?

谢谢

最佳答案

你可以这样做:

$('#defaultModal').on('show.bs.modal', function (event) {
    var button  = $(event.relatedTarget); // Button that triggered the modal 
    var modal       = $(this);
    var title = button.data('title'); alert(title);
    modal.find('.modal-title').text(title)
});

和你的按钮:

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#defaultModal" data-title="New Data" ><i class="glyphicon glyphicon-plus"></i></button>

fiddle :

http://jsfiddle.net/bhumi/hw154nda/1/

关于jquery - 设置模态标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196011/

相关文章:

javascript - 动画不会消失

css - 如何在 mvc 应用程序的编辑器模板模型对象上应用样式

JQuery - 将类添加到不包含类名的每个表备用表行

javascript - 在 dataTable 中的 JavaScript 处理中禁用按钮

javascript - Onchange bootstrap datepicker更改另一个bootstrap datepicker的值

php - 闰年和非闰年分表

jquery - 单击按钮时模态弹出窗口会自动关闭(在生产中仅在我的本地工作正常)

css - .modal-header 显示 1px 白色边框

twitter-bootstrap - Bootstrap - 在另一个模态打开时打开模态并将 'modal-open' 类保留在正文中

javascript - 为什么 .html() 方法不加载元素中的数据?