亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

循環中的多個模態顯示相同的信息而不是不同的信息

循環中的多個模態顯示相同的信息而不是不同的信息

慕運維8079593 2023-12-19 10:40:42
我正在嘗試創建一個網頁,其中包含一堆帶有數據庫信息的卡片。每張卡片上都會有一個“更多信息”按鈕將創建一個彈出模式,其中包含與該卡片相同的信息以及更多信息。我知道我需要一個 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 modalvar modal = document.getElementById("myModal<?php echo $test['id']; ?>");// Get the button that opens the modalvar btn = document.getElementById("myBtn<?php echo $test['id']; ?>");// Get the <span> element that closes the modalvar 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 modalspan.onclick = function() {  modal.style.display = "none";}
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

因此,此代碼的問題在于,它會在循環的每次迭代中生成一個新的?script?元素,該元素會覆蓋所有變量和事件偵聽器。

我建議采用不同的方法來做到這一點。

  1. 將卡片和模態降價留在循環中

  2. 向模態添加一個唯一的 id,事實上,您已經擁有了<div id="myModal<?php echo $test['id']; ?>"></div>

  3. 現在讓我們為按鈕添加一個新屬性,如下所示<button data-target="#myModal<?php echo $test['id']; ?>"></button>

  4. 從循環中刪除?script?標記,并為?所有?按鈕編寫一個新的事件偵聽器,該事件偵聽器將僅顯示?id?屬性等于所單擊按鈕的?data-target?屬性的模態

最終結果如下所示

<?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 提供了一種更簡單的方式來使用模態框并提供所有這些功能。


查看完整回答
反對 回復 2023-12-19
  • 1 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號