出于某种原因,每次我尝试计算表中的行数时 它总是返回 1。我正在动态添加和删除行 表,所以我开始认为它只是计算行数 最初在表中配置。这是我正在使用的代码。
$(".elementDelRowButton").live ('click', function (event) {
console.log ($(this).closest('table').length);
if ($(this).closest('tr').index()!=0) {
$(this).parent().parent().remove();
}
});
我尝试过使用大小、长度和其他变量,它总是返回 1。
这是 HTML:
<table id="element_items"><tr>
<td><input type="radio" name="radio" /></td>
<td><input type="text" value="RadioItem"/></td>
<td><span class="elementAddRowButton">+</span>/<span class="elementDelRowButton">-</span></td>
</tr>
</table>
最佳答案
您的源中可能有一个 tbody 围绕您的所有行。试试这个:
console.log($(this).closest('table').find('tr').length);
顺便说一下,“this.parent().parent().remove()”看起来很危险。您可能希望将选择器与“最接近”函数一起使用。
关于jQuery 表计数行始终返回值 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2364510/