循环遍历表格行,如果复选框选中则返回列值
循环遍历表格行,如果复选框选中则返回列值
这个问题已经在此处有答案:
如何仅在使用jquery检查该行复选框选中时获取序列列中的值? 我目前可以获取系列值,但不确定如何添加复选框条件。
Html:
Name | Serial | Status | Enabled |
---|---|---|---|
Name 1 | 111111 | Online | |
Name 2 | 222222 | Offline | |
Name 3 | 333333 | Online |
Jquery:
$('#usersTable > tbody > tr').each(function (index, tr) { console.log($(this).find('td:nth-child(2)').text()); });
admin 更改状态以发布 2023年5月23日
有许多方法可以完成这个任务,但希望这个解决方案对您有用。
解决方案的要点是遍历每一行,您已经做到了,然后仅在该行的复选框被选中时记录第二列的文本。
$('#usersTable > tbody > tr').each(function() { const currentRow = $(this); const lastColumn = currentRow.find('td:last-child') if (lastColumn.find('input').attr('checked')) { console.log(currentRow.find('td:nth-child(2)').text()); } });