Bootstrap多个模态按钮

时间:2015-02-01 03:31:19

标签: javascript jquery twitter-bootstrap

我有四个网格,在每个网格中我都有一个按钮,我想拥有自己独特的模态。每当我尝试使用Bootstrap的模态'但是,我只是显示第一个按钮的数据,即使我可能点击了应该显示不同模态的其他按钮。

我第一次使用模态,所以我直接根据Bootstrap网站上的示例进行操作。我有以下代码被复制四次,唯一的区别是按钮的名称,模态标题和模态体。任何人都可以告诉我我需要改变什么才能在同一页面中有四个独特的模态?

<div class="col-md-3">
        <div class="head">
            <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Button1</button>
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            <h4 class="modal-title" id="myModalLabel">First Log</h4>
                        </div>
                        <div class="modal-body">
                            Test
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="mediumSpacer"></div>
    </div>

1 个答案:

答案 0 :(得分:6)

每个模态都应该有不同的id,每个按钮的数据目标应该是每个模态的id。

示例:

 <div id="myModal1"class="modal fade" tabindex="-1">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                        <div class="modal-header">....</div>
                        <div class="modal-body">...</div>
                        <div class="modal-footer">...</div>
                </div>
            </div>
 </div>
 <div id="myModal2"class="modal fade" tabindex="-1">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                        <div class="modal-header">....</div>
                        <div class="modal-body">...</div>
                        <div class="modal-footer">...</div>
                </div>
            </div>
 </div>

每个按钮应该是这样的:

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1">Button1</button>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal2">Button1</button>