简体   繁体   English

触发循环生成的多个弹出窗口

[英]Triggering multiple popups generated by loop

I want to display an alert message inside a loop fetching records from the database.我想在从数据库中获取记录的循环中显示一条警报消息。 The problem is, the popup only works for one item and the other item shows no popup.问题是,弹出窗口仅适用于一项,而另一项不显示弹出窗口。

What's going wrong?怎么了?

PHP: PHP:

$query = "SELECT * FROM discount
          WHERE consecutivedays <=  DATEDIFF('$date2','$date1')
                AND idbeach = '$idbeach'
          ORDER BY consecutivedays desc
          LIMIT 1";

$results = mysqli_query($conn, $query);

while($row = $results->fetch_assoc()){

    $reserved= $row['discountperc'];

    if ($reserved=="yes") {

        $getbooking = new WA_MySQLi_RS("getbooking",$sis,1);

        $getbooking->setQuery("SELECT `name`,
                                      CONCAT(`datein`,' - ',`dateout`) AS dates,
                                      price,discount,comment
                               FROM booking
                               where idseatbeach = '$idseatbeach'
                               order by datein limit 1");

        $getbooking->execute();
        $name=$getbooking->getColumnVal("name");
        $dates=$getbooking->getColumnVal("dates");
        $price=$getbooking->getColumnVal("price");  
        $discount=$getbooking->getColumnVal("discount");    
        $comment=$getbooking->getColumnVal("comment");  
        $message = "Booked by: $name\n
                    Date range: $dates\n
                    Price :$price\n
                    Discount :$discount\n
                    Comment :$comment";

        ?>
        <div class="item" >
            <div class="popup" onclick="myFunction()">
                <span class="popuptext" id="myPopup"><?php echo $message;?></span>
                <img src="images/umbrelladisactive.png" width="35" height="35" 
                    alt="<?php echo $nameseat; ?> "/>
                <p style="margin-right: 0px; color: blue;">Currently Booked</p>
            </div>
        </div>
       <?php

   }
}

JavaScript: JavaScript:

var stile = "top=10, left=10, width=450, height=350,
             status=no, menubar=no, toolbar=no scrollbars=no";

function myFunction() {
  var popup = document.getElementById("myPopup");
  popup.classList.toggle("show");
}

CSS: CSS:

.item {
  width: 100px;
  text-align: center;
  display: block;
  background-color: transparent;
  border: 1px solid transparent;
  margin-right: 10px;
  margin-bottom: 1px;
  float: left;
}

#index-gallery {
  width: 50px;
}


/* Popup container */

.popup {
  position: relative;
  display: inline-block;
  cursor: pointer;
}


/* The actual popup (appears on top) */

.popup .popuptext {
  visibility: hidden;
  width: 160px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 8px 0;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -80px;
}


/* Popup arrow */

.popup .popuptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}


/* Toggle this class when clicking on the popup container (hide and show the popup) */

.popup .show {
  visibility: visible;
  -webkit-animation: fadeIn 1s;
  animation: fadeIn 1s
}


/* Add animation (fade in the popup) */

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

An ID in HTML must be unique to the document, meaning that you can't repeat it in a loop. HTML 中的ID对于文档必须是唯一的,这意味着您不能在循环中重复它。 I recommend using a class instead, so it can be repeated.我建议改用一个,这样它就可以重复。

You'll need to popup the element that's associated with (nested inside) whichever element was clicked.您需要弹出与(嵌套在内部)单击的任何元素相关联的元素。 I recommend adding an event listener to each .popop that will fire a handler function upon click.我建议为每个.popop 添加一个事件监听器,它会在点击时触发一个处理函数。 The function should find the popup text within the clicked element by using querySelector and toggle its "show" class.该函数应该通过使用querySelector并切换其“显示”类来查找被单击元素中的弹出文本。

Here's an rudimentary example:这是一个基本的例子:

 // define a function to show a popup's popuptext. function popItUp() { this.querySelector('.popuptext').classList.toggle("show"); } // define all popup elements. let popups = document.querySelectorAll('.popup'); // add listener to each popup element, which binds handler function to click event. popups.forEach( popup => popup.addEventListener('click', popItUp) ); /* // The arrow function above is equivalent to: popups.forEach(function(popup){ popup.addEventListener('click', popItUp); }); */
 .item { width: 100px; text-align: center; display: block; background-color: transparent; border: 1px solid transparent; margin: 80px 0 0 50px; float: left; } .popup { position: relative; cursor: pointer; } .popup .popuptext { visibility: hidden; width: 160px; background-color: #555; color: #fff; text-align: center; border-radius: 6px; padding: 8px 0; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -80px; } .popup .popuptext::after { content: ""; position: absolute; top: 100%; left: 50%; margin-left: -5px; border-width: 5px; border-style: solid; border-color: #555 transparent transparent transparent; } .popuptext.show { visibility: visible; -webkit-animation: fadeIn 1s; animation: fadeIn 1s } @-webkit-keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
 <div class="item"> <div class="popup"> <span class="popuptext">Popup Message 1</span> <p>Currently Booked</p> </div> </div> <div class="item"> <div class="popup"> <span class="popuptext">Popup Message 2</span> <p>Currently Booked</p> </div> </div> <div class="item"> <div class="popup"> <span class="popuptext">Popup Message 3</span> <p>Currently Booked</p> </div> </div>

There are likely some other layout issues with the CSS, but this might give you an idea of the JavaScript implementation. CSS 可能还有一些其他布局问题,但这可能会让您对 JavaScript 实现有所了解。


For reference:以供参考:

The id global attribute defines an identifier (ID) which must be unique in the whole document. id全局属性定义了一个标识符 (ID),它在整个文档中必须是唯一的。

The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors. Document 方法querySelectorAll()返回一个静态(非活动)NodeList,表示与指定选择器组匹配的文档元素列表。

The EventTarget method addEventListener() sets up a function that will be called whenever the specified event is delivered to the target. EventTarget 方法addEventListener()设置了一个函数,每当指定的事件传递到目标时都会调用该函数。

The querySelector() method of the Element interface returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors. Element 接口的querySelector()方法返回第一个元素,该元素是调用它的元素的后代,该元素与指定的选择器组匹配。


Incidentally, I also notice that your query includes LIMIT 1 , so the while loop will not iterate more than once.顺便说一句,我还注意到您的查询包括LIMIT 1 ,因此while循环不会重复多次。 You may want to remove (or increase) that limit:您可能想要删除(或增加)该限制:

$query = "SELECT * FROM discount
          WHERE consecutivedays <=  DATEDIFF('$date2','$date1')
                AND idbeach = '$idbeach'
          ORDER BY consecutivedays desc;";

In HTML, the id attribute was designed to be unique in a document.在 HTML 中, id属性被设计为在文档中是唯一的。 document.getElementById() will always only return one element. document.getElementById()将始终只返回一个元素。

One way to solve your problem is to give each popup a unique id , and pass this id on to myFunction() so it knows which popup to display (I've removed all the in-between lines of code for the sake of brevity).解决您的问题的一种方法是给每个弹出窗口一个唯一的id ,并将这个id传递给myFunction()以便它知道要显示哪个弹出窗口(为了简洁起见,我已经删除了所有中间的代码行) .

I assume your database records have some unique identifier along with name , dates , price , discount and column but since you haven't selected it in your current code I don't know for sure.我假设您的数据库记录具有一些唯一标识符以及namedatespricediscountcolumn但由于您尚未在当前代码中选择它,因此我不确定。 If it doesn't, one alternative is to keep track of a unique counter yourself, such as $id here:如果没有,另一种选择是自己跟踪一个唯一的计数器,例如$id此处:

$id = 0;
while($row = $results->fetch_assoc()){
    $id++;

Then you can send that $id value on to your function and your popup id attribute to help Javascript figure out what you want:然后,您可以将该$id值发送到您的函数和您的弹出窗口id属性,以帮助 Javascript 找出您想要的内容:

<div class="popup" onclick="myFunction('myPopup<?php echo $id; ?>')">
    <span class="popuptext" id="myPopup<?php echo $id; ?>"><?php echo $message;?></span>

Your function should then be modified to understand that change:然后应该修改您的函数以了解该更改:

function myFunction(elementId) {
    var popup = document.getElementById(elementId);
    popup.classList.toggle("show");
}

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

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