简体   繁体   English

jQuery:遍历所有表行并显示具有特定类的每个单元格的弹出窗口

[英]JQuery: Iterate through all table rows and display popup for each cell with a certain class

Using JQuery, I am trying to iterate through all rows of a table and display a timed popup for every cell that has the class = "cell-which-triggers-popup". 使用JQuery,我试图遍历表的所有行,并为每个具有class =“ cell-which-triggers-popup”的单元格显示定时弹出窗口。

The JQuery function below is only displaying a popup for the first cell found. 下面的JQuery函数仅显示找到的第一个单元格的弹出窗口。 How can I get it to display a popup for every cell with that class. 如何获取该类的每个单元格的弹出窗口。

I have a working example here - jsfiddle 我在这里有一个工作示例-jsfiddle

HTML: HTML:

    <div id="popup" data-name="name" class="dialog" title="Bar Crossing Alert!">
    <p></p>            
</div>
<table id="1" border="1">
     <tr>
       <th>Trip ID</th>
       <th>Status</th>
    </tr>
     <tr>
       <td class="cell-with-id">585</td>
       <td class="cell-which-triggers-popup">bar x</td>
    </tr>
         <tr>
       <td class="cell-with-id">444</td>
       <td class="closed">closed</td>
    </tr>
         <tr>
       <td class="cell-with-id">007</td>
       <td class="cell-which-triggers-popup">bar x</td>
    </tr>
         <tr>
       <td class="cell-with-id">987</td>
       <td class="open">open</td>
    </tr>
</table>

JQuery: JQuery的:

      $("tbody tr td.cell-which-triggers-popup").each(function() {
   var cell_value = $(".cell-with-id").html();
              setInterval(function() {
        showPopup(cell_value)    
     }, 3000);
  });

    function showPopup(tr_id){
        $("#popup").dialog({
            width: 300,
            /*height: auto,*/
            resizable: false,
                show: 'blind',
                hide: 'blind',
            open: function(){
                $(this).find("p").html('At Least 10 minutes has expired.<br />Please check the status of the<br />current Bar Crossing.<br />Trip Report ID: ' + tr_id)
            }
        });
    }

This is because the dialog is presenting the element with id="popup", and there's only one. 这是因为对话框显示的元素具有id =“ popup”,并且只有一个。 If you want to pop up several dialogs, you'll need to create a new element each time: 如果要弹出几个对话框,则每次都需要创建一个新元素:

var $dialog = $("#popup").clone();
$dialog.dialog( DIALOG_OPTIONS );

$(".cell-with-id") will select the first element from the matches. $(“。cell-with-id”)将从匹配项中选择第一个元素。 But what you need is the sibling td of the clicked element. 但是您需要的是clicked元素的同胞td。 Instead of using 'each' you can use a click event handler. 代替使用“每个”,您可以使用单击事件处理程序。 And why do you need a setInterwel? 为什么需要setInterwel? This will trigger the showPopup in each 3000 ms. 这将在每3000毫秒触发一次showPopup。 Even if user closes the popup it will reappear. 即使用户关闭了弹出窗口,它也会重新出现。

$("tbody tr td.cell-which-triggers-popup").click(function() {
      var cell_value = $(this).siblings(".cell-with-id").html();
        showPopup(cell_value)    

}); });

working fiddle jsfiddle 工作小提琴jsfiddle

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

相关问题 如何遍历表格行并使用 jQuery 获取单元格值 - How to iterate through a table rows and get the cell values using jQuery 如何遍历表行,更改类,并使用“n”行执行所有操作并了解每行中FIRST单元格的值 - How to loop through table rows, change class, and do it all with “n” number of rows and knowing the values of the FIRST cell in each row 如何从 mysql 表中获取所有行,发送 json,遍历数组并显示? - How do I get all the rows from a mysql table, send json, iterate through array and display? 想用 Puppeteer 刮桌子。 如何获取所有行,遍历行,然后为每一行获取“td”? - Want to scrape table using Puppeteer. How can I get all rows, iterate through rows, and then get "td's" for each row? 迭代每个单元格javascript / Jquery的表更新货币 - Iterate over table updating currency for each cell javascript / Jquery 如何使用JQuery迭代表行并访问一些单元格值? - How to iterate a table rows with JQuery and access some cell values? JQuery 遍历一个表 - JQuery iterate through a table 使用 jQuery 遍历并删除空的 HTML 表格行 - Iterate through and delete empty HTML table rows with jQuery jQuery .each不能遍历类的所有元素 - JQuery .each not looping through all elements with class 如何在jQuery中获取具有特定类的所有表行的总和? - How can I get the sum of all table rows with a certain class in jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM