如何将行中的id传递给单击模态

时间:2018-02-22 23:02:07

标签: php modal-dialog

我想从点击的行中传递id,但对我来说它不起作用

<?php foreach($cadeiras as $ca):?>
     <tr>
         <td><?=$ca['id']?></td>
         <td><?=substr($ca['titulo'], 0, 30)?></td>
         <td><?=$ca['semestre']?></td>
         <td><?=substr($ca['ementa'],0, 45).'...'?></td>
         <td><a href="#exclusao" data-toggle="modal" class="btn btn-danger glyphicon glyphicon-trash btn-block open"></a></td>
     </tr>

<?php endforeach ?>

模态

   <div class="modal fade " id="exclusao"  tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
             <div class="modal-content">
                  <div class="modal-header">
                       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                       <h4 class="modal-title">Excluir cadeira</h4>
                  </div>
                  <form action="database/negarusuario.php" method="POST">
                        <div class="modal-body edit-content">
                             <h3>Tem certeza que deseja excluir a cadeira</h3>     
                        </div>
                        <div class="modal-footer">
                             <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                             <button type="submit" class="btn btn-danger">Negar cadastro</button>
                        </div>
                  </form>
              </div><!-- /.modal-content -->
         </div><!-- /.modal-dialog -->
   </div><!-- /.modal -->

你能帮助我,告诉我如何点击当前行中的$ca['id']

2 个答案:

答案 0 :(得分:0)

<script>
function myFunction() {
    var x = document.getElementsByTagName("H1")[0].getAttribute("modalid");
}
</script>

     <td><a href="#exclusao" data-toggle="modal" modalid="<?php echo $ca['id']; ?>" class="btn btn-danger glyphicon glyphicon-trash btn-block open"></a></td>

试试这段代码

答案 1 :(得分:-1)

添加数据属性data-id="<?=$ca['id']?>"

<td><a href="#exclusao" data-toggle="modal" data-id="<?=$ca['id']?>" class="btn btn-danger glyphicon glyphicon-trash btn-block open"></a></td>

在jQuery上

$('#exclusao').on('show.bs.modal', function(e) {
var id = $(e.relatedTarget).data('id');

});

说明:

  1. 使用数据属性https://www.w3schools.com/tags/att_global_data.asp

  2. 模式显示并隐藏https://getbootstrap.com/docs/3.3/javascript/#modals

  3. relatedTarget https://www.w3schools.com/jquery/event_relatedtarget.asp