【问题标题】:jQuery table count rows always returns value of 1jQuery 表计数行总是返回值 1
【发布时间】:2017-05-24 00:26:06
【问题描述】:

由于某种原因,我每次尝试计算表中的行数 它总是返回 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>

【问题讨论】:

    标签: jquery count html-table rows


    【解决方案1】:

    可能是您的源中有一个 tbody 围绕您的所有行。试试这个:

    console.log($(this).closest('table').find('tr').length);
    

    顺便说一句,“this.parent().parent().remove()”看起来很危险。您可能希望将选择器与“最近”功能一起使用。

    【讨论】:

      【解决方案2】:

      console.log ($(this).closest('table').length);只返回一个包含最接近标签“table”的jquery集合......该集合确实包含一个元素,即表本身,因为最接近返回匹配的第一个父级

      console.log ($(this).closest('table').children("tr").length 应该给出行数

      【讨论】:

        【解决方案3】:

        谢谢大家..这对我的情况有用:

        console.log($(this).closest('table').find('tr').length);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-17
          • 1970-01-01
          • 2013-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多