简体   繁体   English

将ID传递给引导程序模式?

[英]Pass ID to bootstrap modal?

I'm using squarespaceModal from this site 我正在从此站点使用squarespaceModal

I would like to know if is it possible to pass the id to the code of the modal ? 我想知道是否可以将id传递给模态代码? This is the button who's calling the modal : 这是调用模式的按钮:

<div class="center">
    <button id="<?php echo $i; ?>" data-toggle="modal" data-target="#squarespaceModal" class="update_pro btn btn-primary center-block">
        update
    </button>
</div>

This is the first line of the modal : 这是模态的第一行:

<div class="modal fade" id="squarespaceModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">

That is just a standard Bootstrap Modal I dont see anything special about it. 那只是一个标准的Bootstrap Modal,我看不到任何特别之处。 Therefore you have all the events you would like from the modal. 因此,您可以从模式中获得所有想要的事件

So you "could" write an event to trap everytime a modal is opened. 因此,您“可以”编写一个事件以在每次打开模式时捕获该事件。

 $(function() { $('#myModal').on('shown.bs.modal', function() { alert($(this).attr('id')) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/> <button id="myButton" data-target="#myModal" data-toggle="modal" class="btn btn-primary">Open</button> <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel" id="myModal"> <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" id="gridSystemModalLabel">Modal title</h4> </div> <div class="modal-body"> This is my modal body </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><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> 

On your button click set the require value into your input fields of modal and then show modal using below code. 在您的按钮上单击,将所需值设置为模态的输入字段,然后使用以下代码显示模态。

$(".update_pro").click(function () {
    $('#txtYourTexbBoxWithinModal').val($(this).data('id')); // or something eslse.
    $('#squarespaceModal').modal('show');
});

If come across issue of displaying modal then remove 如果遇到显示模式的问题,请删除

data-target="#squarespaceModal" 数据目标=“#squarespaceModal”

Cheeerssss Cheeerssss

Here you go with a solution https://jsfiddle.net/32jsq6aL/ 在这里您可以找到解决方案https://jsfiddle.net/32jsq6aL/

 $('button[data-toggle="modal"]').click(function(){ $($(this).data('target')).attr('btn-id', $(this).attr('id')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="center"> <button id="test 1" data-toggle="modal" data-target="#squarespaceModal" class="update_pro btn btn-primary center-block">update</button> </div> <div class="modal fade" id="squarespaceModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true"> </div> 

OnClick on the button , get the data-target and attach an attribute btn-id as button id to the targeted modal . OnClick上的button ,得到data-target和附加属性btn-id作为button id到目标modal

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM