如何在模态

时间:2015-10-16 07:50:25

标签: javascript html5 bootstrap-modal

我需要在模态中传递data属性。在src中附加图像标记单击链接时。以下是我的代码。我很迷惑。我知道我们可以使用JavaScript onclick方法通过引用传递。还有人可以给我可执行代码。我的代码如下。我不擅长制作JavaScript函数。



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<a href="#checkImage" data-toggle="modal" data-myimage="imageNamefromDataBase.jpg">  Image </a>
<div class="modal fade" id="checkImage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" style="color:black;" id="myModalLabel">View Image</h4>
      </div>
      <div class="modal-body" style="color:black;">
        <h1>Fee Submit</h1>
        <img src="#" alt="bank_chalan" id="thanks" />
      </div>
      <div class="modal-footer">       
       <button type='submit' data-dismiss="modal" name='submit' class='btn btn-success'>Close</button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

如果您想将图像包含在bootstrap模式中,请尝试这样的操作。

<a id = 'checkImage' >Image</a>
<div id = 'imagemodal' class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">View Image</h4>
      </div>
      <div class="modal-body">
        <h1>Fee Submit</h1>
        <img src="http://www.userlogos.org/files/logos/Karmody/alaTest_Iphone01a.png" id="imagepreview" >
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
 </div>

在你的JS文件中,添加这个

$(function() {
    $('#checkImage').on('click', function() {
        $('.imagepreview').attr('src', $(this).find('img').attr('src'));
        $('#imagemodal').modal('show');   
    });     
});

答案 1 :(得分:1)

您可以使用bootstrap模态event侦听器showshown并将数据属性值传递给<img>

$('#checkImage').on('show.bs.modal', function (e) {
    var img = $(e.relatedTarget).data('myimage');
    //alert(img);
    $("#thanks").attr("src",img);
});

工作示例

	$('#checkImage').on('show.bs.modal', function (e) {
	    var img = $(e.relatedTarget).data('myimage');
	    //alert(img);
	    $("#thanks").attr("src", img);
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<a href="#checkImage" data-toggle="modal" data-myimage="http://tympanus.net/Tutorials/CaptionHoverEffects/images/1.png" class="btn btn-default">  Image </a>

<div class="modal fade" id="checkImage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                 <h4 class="modal-title" style="color:black;" id="myModalLabel">View Image</h4>

            </div>
            <div class="modal-body" style="color:black;">
                 <h1>Fee Submit</h1>

                <img src="#" alt="bank_chalan" id="thanks" />
            </div>
            <div class="modal-footer">
                <button type='submit' data-dismiss="modal" name='submit' class='btn btn-success'>Close</button>
            </div>
        </div>
    </div>
</div>

Fiddle

Multiple Images