Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery-获取表体中的行数_Jquery - Fatal编程技术网

JQuery-获取表体中的行数

JQuery-获取表体中的行数,jquery,Jquery,我有一个HTML表,定义如下: <table id="myTable" cellpadding="0" cellspacing="0"> <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>Birth Date</th> </tr> </thead>

我有一个HTML表,定义如下:

<table id="myTable" cellpadding="0" cellspacing="0">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Birth Date</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>John</td>
      <td>Smith</td>
      <td>03-11-1980</td>
    </tr>
  </tbody>
</table>

名字
姓
出生日期
约翰
史密斯
03-11-1980
我想了解如何确定表的
tbody
部分中有多少行,而不是一般的表。如何使用JQuery确定表的tbody部分中有多少行

谢谢大家!

你试过

$("#myTable tbody tr").length
更新:如注释中所述-上面的示例不考虑嵌套表。如果您还想涵盖这一点,您应该使用

像这样:

$("#myTable > tbody > tr").length
选择器选择作为
a
元素的直接子元素的所有
b
元素。因此,此选择器将不匹配嵌套表中的行

var amountOfRows = $("#myTable  tbody  tr").length;
alert(amountOfRows);
变量AMONTOFROWS将保存行数。您还可以使用$(“#myTable tbody tr”).size 但这是长度的捷径


如果在嵌套表格上使用,请参见SLaks answer

有几种不同的方法可以做到这一点,这里有两种方法

 var totalRows = ​$('tbody > tr > td').​​​​​length

 var totalRows2 = $('tbody').find('td').length

vartotalRowstotalRows2将具有
tbody

内的
'td'
数量,如果您没有表id或只想概括表,请使用此选项

$('table.table').find('tbody').children().length;

length
不是一个函数。而且,这不会处理嵌套表。@SLaks-是的,在我键入它后意识到我错了。更新了我的答案以解决长度问题。在下一个表上打了个漂亮的电话-你的答案得到了我的投票:)这也不会处理嵌套表。哇!那很酷。我不知道你可以在JQuery中这样做。JQuery继续给我留下深刻印象。谢谢你分享你的知识!谢谢,这解决了我的问题。当您使用jquery createElement()函数创建表时,它很有用。
$('table.table').find('tbody').children().length;