[英]Using a jQuery.each Loop
So I'm working on a site with one main page containing a table and buttons to control the selected rows in the table. 因此,我正在一个站点上工作,该站点的一个主页包含一个表和用于控制表中所选行的按钮。 Each row in the table has a button with either a + or a - depending on whether the user has clicked it or not.
表格中的每一行都有一个带有+或-的按钮,具体取决于用户是否单击了它。 It starts as a +, then when clicked it adds an identifier unique to that row to an array.
它以+开头,然后在单击时将该行的唯一标识符添加到数组中。 The buttons have an ID equal to the unique identifier.
这些按钮的ID等于唯一标识符。
I want the table to auto-refresh every 10 seconds, but when I do, it resets all buttons to + because that's what is in the HTML. 我希望表格每10秒自动刷新一次,但是当我这样做时,它会将所有按钮重置为+,因为这就是HTML中的内容。
I'm trying to get it to reset the already selected ones to minuses based on the array with this code: 我试图让它根据具有以下代码的数组将已经选择的重置为负数:
function refreshTable(){
$('#tablefill').load('table.php', function(){
setTimeout(refreshTable, 10000);
});
$.each(selected, function(index, value) {
document.getElementById(value).innerHTML = '-';
});
}
I'm not too experienced with jQuery, so I'm sure I'm just missing something. 我对jQuery不太了解,所以我确定我只是缺少一些东西。 Let me know if I should include any other code.
让我知道是否应包含任何其他代码。
Your jQuery code actually looks ok to me. 实际上,您的jQuery代码对我来说还不错。 I think your problem is that you're executing your .each immediately after you've called the server, not after the server has returned;
我认为您的问题是,在调用服务器后立即执行.each,而不是在服务器返回后才执行。 the .each should be inside the callback function you've defined aboved, immediately above or below your setTimeout call, ie:
.each应该位于您上面定义的回调函数之内,紧接在setTimeout调用之上或之下,即:
function refreshTable(){
$('#tablefill').load('table.php', function(){
$.each(selected, function(index, value) {
document.getElementById(value).innerHTML = '-';
});
setTimeout(refreshTable, 10000);
});
}
selected.html('-');
should just work fine, assuming selected
is the result of somthing like $('.yourClass');
假设
selected
是$('.yourClass');
类的结果,应该可以正常工作$('.yourClass');
In jQuery nearly every method works on a collection as well as on single items. 在jQuery中,几乎所有方法都可用于集合以及单个项目。 No need for looping over the items.
无需循环查看项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.