我有這個while循環..通PHP變量模式引導
while($list = $cars->fetch(PDO::FETCH_ASSOC)) {
<div class="car-box modalButton" id="tbinfo" data-title="<?php echo $list['title'] ?>" data-toggle="modal" data-src="http://url.com/car.php?q=<?php echo $list['rand_num']; ?>" data-height="<?php echo $height; ?>" data-width="<?php echo $size[0]; ?>" data-target="#preview">
<div class="modal fade" id="preview" 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">×</button>
<h4 class="modal-title" id="myModalLabel"><?php echo $list['title']; ?></h4>
</div>
<div class="modal-body">
<iframe style="display:block;" frameborder="0"></iframe>
</div>
<div class="modal-footer">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
}
所以,當我在列表中的第一輛車點擊我的模態彈出打開並顯示標題正常的,因爲它是。當我點擊第二輛車,模態彈出窗口打開時,它再次顯示我第一輛車的標題,而不是我點擊的第二輛車。我試圖用jquery取得#tbinfo上的標題點擊沒有成功:
$('#tbinfo').on('click',function(){
var title = $(this).attr('data-title');
$('.modal-title').html(title);
});
我該如何解決它?有任何想法嗎?
您正在使用'while'循環,並在裏面指定'id'給div,這也會重複。檢查你的檢查元素選項卡,你將有'id =「tbinfo」'多個'div'。這可能是爲什麼點擊第二輛車不按預期工作的原因之一。 – Apb
是的,但每次數據標題不同,因爲它在一個while循環..:/ –
好的。但是你正在做'$('#tbinfo')。on('click',function(){'。它總是以'id ='tbinfo''取代第一個div,而不是'id'使用'class'。 – Apb