Javascript 统计表Jquery中的列数和行数

Javascript 统计表Jquery中的列数和行数,javascript,jquery,Javascript,Jquery,我有一张这样的桌子: 我如何计算行级别1+级别2。。。级别5和行级别1+下一行级别1。。。表格末尾的行总数 对于总行数,我使用以下方法: function totalRow() { var total = 0; $('#test').each(function(){ $('.input-md').each(function(){ if ($(this).val() != '') { total += pars

我有一张这样的桌子:

我如何计算行级别1+级别2。。。级别5和行级别1+下一行级别1。。。表格末尾的行总数

对于总行数,我使用以下方法:

function totalRow()
{
    var total = 0;
    $('#test').each(function(){
        $('.input-md').each(function(){
            if ($(this).val() != '') {
                total += parseInt($(this).val());
            }
        });
    });
    $('#totalall').val(total);
}
html ID在文档中必须是唯一的。类可以重复


$'test'。每个函数{都没有意义,因为文档中只能有一个id测试。它不会在您的行之间循环。

我想问题是您忘记选择主。 你可以写:

$('#test,#primary').each(function(){
    $('.input-md').each(function(){
        if ($(this).val() != '') {
            total += parseInt($(this).val());
        }
    });
});
但是,如果您仅在此表中使用类input md,则可以通过编写以下命令来简化它:

$('.input-md').each(function(){
    if ($(this).val() != '') {
        total += parseInt($(this).val());
    }
});

我解决了我的问题。对于计数行:

function totalRow()
{
    var total = 0;
    $('table .count-row td .cols, .total-row').each(function() {
        if ($(this).prop('disabled')) {
            $(this).val(total);
            total = 0;
        } else {
            if ($(this).val() != '') {
                total += parseInt($(this).val());
            }
        }
    });
}
对于计数列:

function totalColumns()
{
    var id = ["one", "two", "three", "four", "five"];
    var totalCol = 0;
    for (var i=0; i<=5; i++) {
        $('.column-' + id[i]).each(function(){
            if ($(this).val() != '') {
                totalCol += parseInt($(this).val());
            }
            $('#count-' + id[i]).text(totalCol);
        });
        totalCol = 0;
    }
}

正如我所看到的,你的代码可以处理所有问题。你也需要列的总数吗?这与最初的问题不太清楚。你的问题很混乱……一行就是一行。我猜你指的是列和行。