how to pass data-attribute in modal

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

I need to pass the data attribute in my modal. to attach with image tag in src When clicked on the link. Below is my code. I am confused. I know we can use JavaScript onclick method to pass by reference. Still can anyone give me executable code. My code is below. I am not really good in making JavaScript functions.

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

回答1:

If you want to include image(s) into bootstrap modal, try something like this.

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

In you JS file, add this

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


回答2:

you can use bootstrap modal event listener show or shown and pass the data attribute value to <img>

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

Working Example

	$('#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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!