2017-06-01 60 views
0

所以我對jquery相當陌生,我試圖獲取我的模態的動態生成的ID。所以這是我的代碼如下所示:如何獲取Modal的動態生成ID

<?php 
    $sql = "SELECT * FROM currentstatus"; 
    $res = mysql_query($sql); 
    while($r = mysql_fetch_assoc($res)) { 
     echo '<div class="modal fade" id="'.$r['rackName'].'" role="dialog"> 
     <div class="modal-dialog modal-lg" 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="myModalLabel">'.$r['rackName'].'</h4> 
      </div> 
      <div class="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">Print</button> 
      </div> 
     </div> 
     </div> 
    </div>'; 
    } 
    ?> 

$ [ 'rackName']是我的動態ID

我把這種模式,使用下面的代碼:

<a href="" data-toggle="modal" data-target="#'.$r['rackName'].'">'.$r['rackName'].'</a> 

回答

0

你可以捕獲展示活動並獲取ID如下:

$(function(){ 
    $('.modal').on('show.bs.modal', function() { 
     console.log($(this).prop('id')); 
    }); 
}); 
+0

我需要載入ID。可能嗎? –

+0

你先生,是救命恩人!萬分感謝! :) –