简体   繁体   English

如何在Bootstrap 3中仅使用一个模式div打开具有多个按钮的多个模式窗口

[英]How to open multiple modal windows with multiple buttons using only one modal div in Bootstrap 3

Basically I have this code 基本上我有这段代码

<div class="col-lg-3 col-md-3 features">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h1 class="panel-title"><span class="glyphicon glyphicon-user"></span> Intern</h1>
                    </div>
                    <div class="panel-body">
                        <img class="img-circle" src="js/holder.js/170x170" alt="Tony" style="margin-top: 20px; width:170px; height:170px">
                        <h2>Alvin</h2>
                        <p>Biodiesel fixie Etsy distillery ethnic. Occupy Cosby sweater Pitchfork, chia YOLO art party keytar direct trade seitan before 
                        they sold out squid stumptown salvia deep v.</p>
                    </div>
                    <div class="panel-footer panel-primary"><h1 class="panel-title"><span class="glyphicon glyphicon-file"></span> Resume</h1></div>
                </div>
            </div><!-- /.col-lg-4 -->

            <div class="col-lg-3 col-md-3 features">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h1 class="panel-title"><span class="glyphicon glyphicon-user"></span> Intern</h1>
                    </div>
                    <div class="panel-body">
                        <img class="img-circle" src="js/holder.js/170x170" alt="Tony" style="margin-top: 20px; width:170px; height:170px">
                        <h2>Marlon</h2>
                        <p>Biodiesel fixie Etsy distillery ethnic. Occupy Cosby sweater Pitchfork, chia YOLO art party keytar direct trade seitan before 
                        they sold out squid stumptown salvia deep v.</p>
                    </div>
                    <div class="panel-footer panel-primary"><h1 class="panel-title"><span class="glyphicon glyphicon-file"></span> Resume</h1></div>
                </div>
            </div><!-- /.col-lg-4 -->

Now every intern has a button at the footer that will activate a modal showing their resume. 现在,每个实习生在页脚处都有一个按钮,该按钮将激活显示其简历的模式。 What I dont want is to have to add 8 modal divs for each button cause that is just way to much. 我不想要的是必须为每个按钮添加8个模态div,这只是很多方法。 Is there a way I can pass a variable to a modal from each button and have the specific resume open upon specific intern chosen. 有没有一种方法可以将变量从每个按钮传递给模式,并在选择特定实习生后打开特定的履历表。 Here is the modal part to initiate it also. 这也是启动它的模态部分。

<div class="container"> 
    <!-- Button trigger modal -->
    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Modal Launch Button
    </button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <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">
            <object type="application/pdf" width="100%" height="400px" data="CV2014.pdf"></object>
          </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>
</div>

I would hate to have to have 8 of those things. 我讨厌必须拥有其中的8个。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

It's fairly easy. 这很容易。 The idea is to have a hidden input next to each of your modal triggering anchor/button, once clicked, pass the closest hidden input value (which contains the pdf link or any other value) to your modal. 这个想法是在您的每个模态触发锚点/按钮旁边都有一个隐藏的输入,一旦单击,将最接近的隐藏输入值(包含pdf链接或任何其他值)传递给您的模态。

So in every resume, add (I have used an anchor tag here but the same could be done with a button): 因此,在每张简历中,添加(我在这里使用了anchor标签,但也可以通过按钮来完成此操作):

<input type="hidden" value="url" class="pdf-link">                    
<a data-toggle="modal" data-target="#myModal">Open</a>

And the jQuery bit: 和jQuery位:

//on click of the anchor tag
$('a[data-toggle="modal"]').on('click', function (e) {

    //get the pdf link (from the previous element which is a hidden input)
    pdf = $(this).prev('.pdf-link').val();

    //load the pdf in a iframe window placed within the modal
    $('.modal-body').html('<iframe src="'+pdf+'"></iframe>');
});

Have a look at the demo here. 这里看看演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM