将图像传递到bootstrap模态

时间:2016-06-22 08:56:55

标签: javascript ruby twitter-bootstrap

你好,我有一个图像列表。用户可以单击任何图像,它将打开一个bootstrap模式。在这个boostrap模式中,我需要出现用户点击的图像。

请在下面的代码中查看我的评论,以便更清晰。

非常感谢任何帮助。谢谢。

<center>
  <div id="items" class="transitions-enabled">
    <% @images.each do |image| %>
      <div class="box panel panel-default">
        <div class="square_item_image">
          #HERE IS THE IMAGE I WANT TO APPEAR IN THE BOOSTRAP MODAL
          <%= image_tag(image["images"]["low_resolution"]["url"]) %>
        </div>
        <div class="item_info">
          <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Create Outfit</button>
        </div>
      </div>
    <% end %>
  </div>
</center>

<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Create</h4>
      </div>
      <div class="modal-body">
        #I WOULD LIKE THE IMAGE TO APPEAR HERE
        <p>Some text in the modal.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

使用javascript你可以做到

JAVASCRIPT

$(document).on("click", ".open-AddImgDialog", function () {
 var imgsrc = $(this).data('id');
 $('#my_image').attr('src',imgsrc););
});

HTML代码

<div class="item_info">
<button type="button" class="btn btn-info btn-lg open-AddImgDialog" data-id="img/img.jpg" data-toggle="modal" data-target="#myModal">Create Outfit</button>
</div>

MODAL Pop

<div class="modal-body">
    <img id="my_image" />
 </div>

此处data-id =“img / img.jpg”是您的图片路径,并在按钮中添加类名open-AddImgDialog。