最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to load image into bootstrap modal with javascript - Stack Overflow

programmeradmin0浏览0评论

My setup is 4 links (below). I want each link to open myModal, but depending on which link gets clicked, a different image file should load in the modal. I got it working for one of the links.

<li><a href="#myModal" data-toggle="modal">6 Teams</a></li>
<li><a href="#">5 Teams</a></li>
<li><a href="#">4 Teams</a></li>
<li><a href="#">3 Teams</a></li>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width:800px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<img src="/images/brackets/6teamDouble1.jpg">
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>

How do I get this to work for each link without building a separate modal for each one?

My setup is 4 links (below). I want each link to open myModal, but depending on which link gets clicked, a different image file should load in the modal. I got it working for one of the links.

<li><a href="#myModal" data-toggle="modal">6 Teams</a></li>
<li><a href="#">5 Teams</a></li>
<li><a href="#">4 Teams</a></li>
<li><a href="#">3 Teams</a></li>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width:800px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<img src="/images/brackets/6teamDouble1.jpg">
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>

How do I get this to work for each link without building a separate modal for each one?

Share Improve this question asked Sep 12, 2013 at 3:35 BenBen 21.6k34 gold badges126 silver badges211 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 32

By using jquery .click you can easily achieve this (with html5 data attribute too).

<ul>
    <li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey1">6 Teams</a>
    </li>
    <li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey3">5 Teams</a>
    </li>
    <li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey4">4 Teams</a>
    </li>
    <li><a href="#myModal" data-toggle="modal" data-img-url="http://placehold.it/200x200/dddddd/ffffff&text=Hey5">3 Teams</a>
    </li>
</ul>

Script:

$('li a').click(function (e) {
    $('#myModal img').attr('src', $(this).attr('data-img-url'));
});

Fiddle: http://jsfiddle.net/vLSWH/292/

Note: Though you did not specify using jQuery, twitter-bootstrap modal require you to use jQuery anyway, so might as well use it.

发布评论

评论列表(0)

  1. 暂无评论