简体   繁体   English

仅打开第一个模态

[英]Only opens the first modal

I am trying to get this modal script to work, but for some reason only the first div opens and closes pretty sure it has something to do with php. 我正在尝试使该模式脚本起作用,但是由于某些原因,只有第一个div可以打开和关闭,因此可以确定它与php有关。 This code is copied from w3schools and has been edited. 该代码是从w3schools复制而来的,已被编辑。

HTML/PHP: HTML / PHP:

<?php
try
{
$sQuery= "SELECT * FROM maillijst ORDER BY naam ASC";

$oStmt = $db->prepare($sQuery);
$oStmt->execute();

while($rij = $oStmt->fetch(PDO::FETCH_ASSOC))
{
?>

<!-- Modal content -->
<div class="modal-content">
  <div class="modal-header">
    <span class="close">&times;</span>
    <h2><?php echo($rij['naam']); ?></h2>
  </div>

  <div class="modal-body">
    <p><?php echo($rij['couponcode']); ?></p>
  </div>

  <div class="modal-footer">
    <h3><?php echo($rij['email']); ?></h3>
  </div>
 </div>

<?php
}
}
catch(PDOException $e)
{
    $sMsg = '<p>
            Regelnummer: '.$e->getLine().'<br/>
            Bestand: '.$e->GetFile().'<br/>
            Foutmelding: '.$e->getMessage().'
        </p>';

    trigger_error($sMsg);
}
$db=NULL;
?>

JAVASCRIPT: JAVASCRIPT:

 <script>
 // Get the modal
 var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

 // 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";
        }
      }

You try to manipulate element with id="myModal" using your JavaScript. 您尝试使用JavaScript使用id =“ myModal”来操纵元素。 But I haven't found that element in your modal content. 但我尚未在您的模式内容中找到该元素。 There's should be something like: 应该是这样的:

span.onclick = function() {
 $(this).closest('.modal-content').css('display', 'none');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM