2

何らかの理由で、テーブル内の行数をカウントしようとするたびに、常に 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>
4

3 に答える 3

4

ソースにすべての行を囲む tbody がある可能性があります。これを試して:

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

ちなみに、「this.parent().parent().remove()」は危険そうです。「最も近い」関数と一緒にセレクターを使用することもできます。

于 2010-03-02T16:00:41.810 に答える
2

console.log ($(this).closest('table').length); 最も近いタグ「テーブル」を含むjqueryセットを返すだけです...セットには、テーブル自体という1つの要素が含まれます。最も近いものは、一致する最初の親を返します

console.log ($(this).closest('table').children("tr").length は行数を与える必要があります

于 2010-03-02T16:03:34.477 に答える
0

みんなありがとう..これは私の状況でうまくいきました:

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

于 2010-03-02T16:12:37.310 に答える