循环中的多个模态显示相同的信息,而不是不同的信息



我正试图创建一个网页,该网页将包含一堆包含数据库信息的卡片。

每张卡上都会有一个"更多信息"按钮,它将创建一个弹出模式,其中包含与该卡相同的信息+更多信息。我知道我需要一个while循环来生成模态,并单独为模态提供不同的ID。

我已经使用数据库的"id"列完成了必要的操作,它是唯一的&当我点击按钮时,我确实会得到模态,但唯一的问题是,无论我点击哪个按钮,模态都会显示最后一张卡片的信息(如标题、字段、概要、描述(。

所有的卡片都显示了他们的个人信息。听起来我的while循环有问题吗?我确实检查了源代码&它确实正确地显示了每个特定卡片下面的带有自己id的各个模态(带有正确的id,如1、2、3、4。(请提供帮助。

<?php
include("Config.php");              
$query = ("SELECT * FROM proj");            
$result = mysqli_query($conn, $query) or die( mysqli_error($conn));         
while($test = mysqli_fetch_array($result))                              
{                       
?>                  
<div class="card"><div class="container1">
<div class="card-text">  
<h4><b><?php echo $test['title']; ?></b></h4>
<b>field: </b><?php echo $test['field']; ?></br>    
<p><?php echo $test['synopsis']; ?></p>
<!-- Trigger/Open The Modal -->
<button id="myBtn<?php echo $test['id']; ?>">More Info</button>
<!-- The Modal -->    
<div id="myModal<?php echo $test['id']; ?>" class="modal"> 
<!-- Modal content -->
<div class="modal-content">  
<span class="close">&times;</span>
<h4><b><?php echo $test['title']; ?></b></h4>
<p><b>field: </b><?php echo $test['field']; ?></p>      
<p><?php echo $test['synopsis']; ?></p>
<p><?php echo $test['description']; ?></p>  
<script>
// Get the modal
var modal = document.getElementById("myModal<?php echo $test['id']; ?>");
// Get the button that opens the modal
var btn = document.getElementById("myBtn<?php echo $test['id']; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal 
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "NONE";
}
}
</script>   
</div>
</div>      
</div>
</div>
</div>                      
<?php
}
mysqli_close($conn);
?>

因此,此代码的问题在于,它在循环的每次迭代中都会生成一个新的script元素,该元素会覆盖所有变量和事件侦听器。

我建议用不同的方法来做这件事。

  1. 将卡片和模式标记留在循环中
  2. 向模态添加一个唯一的id,事实上,您已经有了<div id="myModal<?php echo $test['id']; ?>"></div>
  3. 现在,让我们为按钮添加一个新属性,如以下<button data-target="#myModal<?php echo $test['id']; ?>"></button>
  4. 从循环中移除script标记,并为所有按钮编写一个新的事件侦听器,该侦听器将仅显示具有与所单击按钮的data-target属性相等的id属性的模态

最终结果看起来像这个

<?php
include("Config.php");              
$query = ("SELECT * FROM proj");            
$result = mysqli_query($conn, $query);         
while($test = mysqli_fetch_array($result))                              
{                       
?>                  
<div class="card"><div class="container1">
<div class="card-text">  
<h4><b><?php echo $test['title']; ?></b></h4>
<b>field: </b><?php echo $test['field']; ?></br>    
<p><?php echo $test['synopsis']; ?></p>
<!-- Trigger/Open The Modal -->
<button data-target="myModal<?php echo $test['id']; ?>" class="action-btn">More Info</button>
<!-- The Modal -->    
<div id="myModal<?php echo $test['id']; ?>" class="modal"> 
<!-- Modal content -->
<div class="modal-content">  
<span class="close">&times;</span>
<h4><b><?php echo $test['title']; ?></b></h4>
<p><b>field: </b><?php echo $test['field']; ?></p>      
<p><?php echo $test['synopsis']; ?></p>
<p><?php echo $test['description']; ?></p>   
</div>
</div>      
</div>
</div>
</div>                      
<?php
}
mysqli_close($conn);
?>
<script>
var buttons = document.querySelectorAll(".action-btn");
// Feel free to use any other way to iterate over buttons
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function (event) {
var modal = (document.getElementById(
event.target.getAttribute("data-target")
).style.display = "block");
});
}
// Add code to close modals
</script>   

注意:Bootstrap提供了一种更简单的方式来使用具有所有这些功能的模态。参见引导模式

最新更新