jquery复选框选中了计数问题

jquery checkbox checked count issue

本文关键字:问题 复选框 jquery      更新时间:2023-09-26

我有一个表,如下所示:http://jsfiddle.net/8SEz2/4/

Branch    StSeq#   Inv#  Invoice Date    Payable Amount     Pay?
Branch1     2       A2   11/11/2011        49,500.00       checkbox(unchecked)
Branch1     3       A3   11/11/2011        12,221.55       checkbox(checked)
Branch3     4       B1   11/11/2011        12,220.56       checkbox(unchecked)

我试图实现的是,当行复选框状态发生更改时,我需要计算共享相同分支名称的复选框的数量。

Ex。如果我点击第一个复选框,(Branch1)我应该得到一个弹出窗口,其中包含选中的分支计数,在这种情况下,它应该是2。

这是我的jquery:

//Please refer to the jsFiddle for the class names
//used to track checked branches
$('input.payMe').change(function() {
    if ($(this).is(':checked')) {
    //cm enable
    var branchName = $(this).parent().prevAll("td.invoiceBranch").html().trim();
    var branchCount = 0;
    $('td.invoiceBranch').each(function(index) {
        if ($(this).html().trim() == branchName) {
            branchCount++;
        }
    });
        alert(branchCount);
    }
    else {
    //cm disable
        var branchName = $(this).parent().prevAll("td.invoiceBranch").html().trim();
        var branchCount = 0;
        $('td.invoiceBranch').each(function (index) {
            if ($(this).html().trim() == branchName) {
                //Needs to select the checkbox to get checked state.
                //Not sure if this is correct
                var inputHTML = $(this).nextAll("td.paymentCheckBox").first();
                if($(inputHTML).is(":checked")) {
                    branchCount++;
                }
            }
        });      
        alert(branchCount);
    }
}); 

有什么想法吗?我被难住了两天。谢谢

我认为应该为每个复选框应用一个带有分支名称的标记

<input type="checkbox" class="payMe" branch="Branch1" ... />

那么函数应该大致类似

$(':checkbox.payMe').change(function() {
  alert($(':checkbox.payMe[branch="' + $(this).attr('branch') + '"]:checked').length);
});

到此为止:http://jsfiddle.net/8SEz2/2/

我将属性数据分支添加到复选框

这是他的js:

$(".payMe").change(function(){
    var branch = $(this).attr('data-branch');        
    alert( $("input[data-branch='" + branch + "']:checked").length );
});