[英]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)
}); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.