如何计算使用jQuery表内TR元素的数量?
我知道有一个类似的问题 ,但我只是想总的行。
如何计算使用jQuery表内TR元素的数量?
我知道有一个类似的问题 ,但我只是想总的行。
使用选择,将选择所有行并采取长度。
var rowCount = $('#myTable tr').length;
注:此方法也算每个嵌套表的所有TRS!
如果你使用<tbody>
或<tfoot>
在你的餐桌,你必须使用以下语法,否则你会得到一个不正确的值:
var rowCount = $('#myTable >tbody >tr').length;
另外...
var rowCount = $('table#myTable tr:last').index() + 1;
的jsfiddle DEMO
这将确保任何嵌套表行是不是也计算在内。
好吧,我从表中的ATTR行,并得到该集合长度:
$("#myTable").attr('rows').length;
我认为jQuery的效果欠佳。
这是我对此采取:
//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
return $('tr', $(this).find('tbody')).length;
};
用法:
var rowCount = $('#productTypesTable').rowCount();
我有以下几点:
jQuery('#tableId').find('tr').index();
我需要一种方法在AJAX回报要做到这一点,所以我写了这片:
<p id="num_results">Number of results: <span></span></p>
<div id="results"></div>
<script type="text/javascript">
$(function(){
ajax();
})
//Function that makes Ajax call out to receive search results
var ajax = function() {
//Setup Ajax
$.ajax({
url: '/path/to/url', //URL to load
type: 'GET', //Type of Ajax call
dataType: 'html', //Type of data to be expected on return
success: function(data) { //Function that manipulates the returned AJAX'ed data
$('#results').html(data); //Load the data into a HTML holder
var $el = $('#results'); //jQuery Object that is holding the results
setTimeout(function(){ //Custom callback function to count the number of results
callBack($el);
});
}
});
}
//Custom Callback function to return the number of results
var callBack = function(el) {
var length = $('tr', $(el)).not('tr:first').length; //Count all TR DOM elements, except the first row (which contains the header information)
$('#num_results span').text(length); //Write the counted results to the DOM
}
</script>
显然,这是一个简单的例子,但它可能会有所帮助。
我发现,如果你想不计数次,并从表内表中的任何行数行这个工作真的很好:
var rowCount = $("#tableData > tbody").children().length;
试试这个,如果有TBODY
无标题
$("#myTable > tbody").children.length
如果头则
$("#myTable > tbody").children.length -1
请享用!!!
row_count = $('#my_table').find('tr').length;
column_count = $('#my_table').find('td').length / row_count;
jQuery("#tablebodyID >tr).length();