我有一个如下所示的表: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)
我想要完成的是,当行复选框状态更改时,我需要计算也被选中且共享相同分支名称的复选框的数量。
例如。如果我单击第一个复选框(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);
}
});
有什么想法吗?我已经困惑了2天了。谢谢!
最佳答案
我认为你应该为每个带有分支名称的复选框应用一个标签
<input type="checkbox" class="payMe" branch="Branch1" ... />
那么函数大致应该是这样的
$(':checkbox.payMe').change(function() {
alert($(':checkbox.payMe[branch="' + $(this).attr('branch') + '"]:checked').length);
});
关于javascript - jquery复选框选中计数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8755227/