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

标签 javascript jquery html twitter-bootstrap

基本上我有这个代码

<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 -->

现在,每个实习生的页脚都有一个按钮,可以激活显示其简历的模式。我不想要的是必须为每个按钮添加 8 个模态 div,因为这太多了。有没有一种方法可以将变量从每个按钮传递给模式,并在选择特定实习生时打开特定简历。这也是启动它的模态部分。

<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>

我不愿意拥有 8 个这样的东西。任何帮助,将不胜感激。谢谢

最佳答案

这相当容易。这个想法是在每个模态触发 anchor /按钮旁边都有一个隐藏输入,单击后,将最接近的隐藏输入值(其中包含 pdf 链接或任何其他值)传递给您的模态。

因此,在每份简历中添加(我在这里使用了 anchor 标记,但也可以使用按钮完成相同的操作):

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

还有 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>');
});

看看 demo here.

关于javascript - 如何在 Bootstrap 3 中仅使用一个模态 div 打开具有多个按钮的多个模态窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029261/

相关文章:

javascript - iOS Safari 在使用麦克风时降低音频播放音量

javascript - 将空数组发送到 webapi

html - 将水平导航更改为垂直

javascript - 如何从单个表单元素内的多个字段集中获取输入字段(包括选择、文本区域)的所有值?

javascript - 如何使用 jQuery 检查元素是否中断到下一行?

javascript - 如何在新窗口中以及右键单击新选项卡/窗口中打开链接?

html - 如何修复背景图片上的重复内容?

javascript - Angular JS http post不写入文件

javascript - SAAS 每席位认证

javascript - 允许小写的正则表达式