调用Bootstrap Modal时显示正确的选项卡

时间:2016-05-25 06:51:04

标签: javascript jquery twitter-bootstrap

我的主页上有三个按钮,有三个标签的模式。我想在单击相应的按钮时启动正确的选项卡。我应该怎么做呢?

例如:我想展示" Careers"如果单击“职业”按钮,则选项卡。同样,对于Contact&合伙人也是。

<div class="row">
<div class="col-xs-4"><a data-toggle="modal" data-target="#myModal"><span class="btn btn-primary">Contact</span></a></div>
<div class="col-xs-4"><a data-toggle="modal" data-target="#myModal"><span class="btn btn-primary">Careers</span></a></div>
<div class="col-xs-4"><a data-toggle="modal" data-target="#myModal"><span class="btn btn-primary">Partners</span></a></div>
</div>

<!-- modal-->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <a href="" class="pull-right" data-dismiss="modal">CLOSE</a>
        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#contact">Contact</a></li>
            <li><a data-toggle="tab" href="#careers">Careers</a></li>
            <li><a data-toggle="tab" href="#partners">Partners</a></li>
        </ul>
      </div>
      <div class="modal-body">

        <div class="tab-content">
            <div id="contact" class="tab-pane fade in active">
                <h3>Contact Form</h3>
            </div>
            <div id="careers" class="tab-pane fade">
                <h3>Careers Form</h3>
            </div>
            <div id="partners" class="tab-pane fade">
                <h3>Partners Form</h3>
            </div>
        </div>
      </div>
    </div>
  </div>
</div>

DEMO

1 个答案:

答案 0 :(得分:5)

只需在每个按钮上添加带有正确标签名称的data-target-tab="#contact"即可。在show.bs.modalbootstrap modal events)上,打开正确的标签(bootstrap tab methods)。

$('#myModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) 
  $("a[href='"+button.data('target-tab')+"']").tab('show');
})

这是一个有效的fiddle