第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

循環(huán)中的多個模態(tài)顯示相同的信息而不是不同的信息

循環(huán)中的多個模態(tài)顯示相同的信息而不是不同的信息

慕運(yùn)維8079593 2023-12-19 10:40:42
我正在嘗試創(chuàng)建一個網(wǎng)頁,其中包含一堆帶有數(shù)據(jù)庫信息的卡片。每張卡片上都會有一個“更多信息”按鈕將創(chuàng)建一個彈出模式,其中包含與該卡片相同的信息以及更多信息。我知道我需要一個 while 循環(huán)來生成模態(tài),并單獨(dú)為模態(tài)提供不同的 ID。我已經(jīng)使用“id”完成了必要的操作數(shù)據(jù)庫中唯一的列當(dāng)我單擊按鈕時,我確實(shí)得到了模態(tài),但唯一的問題是,無論我單擊哪個按鈕,模態(tài)都會顯示最后一張卡片的信息(例如標(biāo)題、字段、概要、描述)。所有的卡片都顯示了它們的個人信息。聽起來我的 while 循環(huán)有問題?我確實(shí)檢查了源代碼&它確實(shí)正確地在每張?zhí)囟ㄆ路斤@示帶有自己 id 的各個模態(tài)(具有正確的 id,例如 1、2、3、4。)請?zhí)峁椭?lt;?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貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個贊

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

我建議采用不同的方法來做到這一點(diǎn)。

  1. 將卡片和模態(tài)降價(jià)留在循環(huán)中

  2. 向模態(tài)添加一個唯一的 id,事實(shí)上,您已經(jīng)擁有了<div id="myModal<?php echo $test['id']; ?>"></div>

  3. 現(xiàn)在讓我們?yōu)榘粹o添加一個新屬性,如下所示<button data-target="#myModal<?php echo $test['id']; ?>"></button>

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

最終結(jié)果如下所示

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


查看完整回答
反對 回復(fù) 2023-12-19
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號