Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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
Javascript jquery复选框已选中计数问题_Javascript_Jquery_Html - Fatal编程技术网

Javascript jquery复选框已选中计数问题

Javascript jquery复选框已选中计数问题,javascript,jquery,html,Javascript,Jquery,Html,我有一张这样的桌子: 我试图做到的是,当行复选框状态发生更改时,我需要计算共享相同分支名称的同时被选中的复选框的数量 例如,如果我点击第一个复选框,(Branch1),我会得到一个带有分支选中计数的弹出窗口,在这种情况下,它应该是2 这是我的jquery: //Please refer to the jsFiddle for the class names //used to track checked branches $('input.payMe').change(function() {

我有一张这样的桌子:

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

例如,如果我点击第一个复选框,(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);
    }
}); 
有什么想法吗?我已经被难倒两天了。谢谢

给你:

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

下面是他的js:

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

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

下面是他的js:

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

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

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

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

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

为什么不将branchname编码为复选框ID或名称的一部分?我考虑过这一点,但这不是我需要的。我在该页中有另一个表,如果选中的分支数为0,则该表的行被禁用。因此,如果选中Branch1的两个复选框中的1,我的另一个表的行将被启用,如果为零,则需要再次禁用它。这就是为什么我需要检查总数。为什么不将branchname编码为复选框ID或名称的一部分?我考虑过这一点,但这不是我需要的。我在该页中有另一个表,如果选中的分支数为0,则该表的行被禁用。因此,如果选中Branch1的两个复选框中的1,我的另一个表的行将被启用,如果为零,则需要再次禁用它。这就是为什么我需要检查总数。回答得好。工作得很好。谢谢你,回答得好。工作得很好。谢谢你,格莱德。