JQuery:迭代遍历所有表行,并显示每个单元格的弹出窗口

问题描述:

使用JQuery,我试图遍历表中的所有行并显示定时弹出窗口对于每个具有类的单元格 =“细胞其中,触发,弹出”。JQuery:迭代遍历所有表行,并显示每个单元格的弹出窗口

下面的JQuery函数仅显示找到的第一个单元格的弹出窗口。我怎样才能得到它显示该类的每个单元格的弹出窗口。

我这里有一个工作的例子 - jsfiddle

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的:

 $("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) 
      } 
     }); 
    } 

这是因为对话与ID = “弹出式” 呈现的元素,只有一个。如果你想弹出多个对话框,你需要在每次创建新的元素:

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

嗨邓肯 - 种类的作品谢谢你。但由于定时器功能而不断循环。 (见小提琴),仍然只显示找到的第一行。 - http://jsfiddle.net/twobears/xaqtawog/491/ – Twobears

+0

更改为setTimeout并停止循环,但仍只返回第一个Trip ID。 - [jsfiddle](http://jsfiddle.net/twobears/xaqtawog/492/) – Twobears

$(“细胞与-ID”)将选择从比赛的第一要素。但是你需要的是单击元素的同胞td。您可以使用点击事件处理程序,而不是使用“每个”。为什么你需要一个setInterwel?这将在每3000毫秒触发showPopup。即使用户关闭弹出窗口,它也会重新出现。

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

});

工作小提琴jsfiddle

+0

试图显示一个弹出窗口作为无线电操作员的提醒,以检查经过一段时间后越过危险酒吧的船的状态。一旦船被清除,无线电操作员可以将状态从'cell-which-triggers-popup'更改为'open'(这不会触发弹出) – Twobears

+0

当无线电操作员改变状态时,你必须使用'clearInterval()'。在此之前,相同的弹出窗口会显示。如何在同一个弹出窗口中显示两个旅程ID? –

+0

每次旅行Id都会涉及一艘可能在不同时间离开或穿过不同酒吧的单独船只,因此需要分别计时并使用自己的弹出式窗口进行识别。我试图在每个船只上有一个弹出窗口,在其单元格状态更改为'cell-which-triggers-popup'后的特定时间出现。 @Duncan Thacker – Twobears