我正在尝试合并两个不同复选框组的计数,并提出它们之间的复选框总数。但是,当我将变量cnt和cnt1放在相应的函数之外时,它会添加复选框的数量,而不是计算它们。
$(document).ready(function () {
var countTotal = 0;
var cnt = 0;
var cnt1 = 0;
$('input[name="chkSelect"]').click(function () {
updateNumber('chkSelect');
});
$('input[name="chkSelect1"]').click(function () {
updateNumber1('chkSelect1');
});
var updateNumber = function () {
var result = $('input[name="chkSelect"]:checked');
var countedgroupno = [];
result.each(function () {
var group = $(this).attr('group');
var lastletter = group.substr(group.length-1, 1);
if(lastletter.match(/[a-z]/i)) {
group = group.substring(0, group.length-1);
}
if ($.inArray(group, countedgroupno) == -1) {
cnt++;
countedgroupno.push(group);
}
});
$('#countCK').val(cnt);
};
var updateNumber1 = function () {
var result = $('input[name="chkSelect1"]:checked');
var countedgroupno = [];
result.each(function () {
var group = $(this).attr('group');
var lastletter = group.substr(group.length - 1, 1);
if (lastletter.match(/[a-z]/i)) {
group = group.substring(0, group.length - 1);
}
if ($.inArray(group, countedgroupno) == -1) {
cnt1++;
countedgroupno.push(group);
}
});
$('#countCK1').val(cnt1);
};
var countTotal = cnt * cnt1
$('#TotalcountCK').val(countTotal);
});
<form id="form1" runat="server">
<br />
Count Group 1: <input type="text" id="countCK" />
<br />
Count Group 2: <input type="text" id="countCK1" />
<br />
Total Count: <input type="text" id="TotalcountCK" />
<br />
<asp:Repeater ID="myRepeater" runat="server">
<HeaderTemplate>
<table style="height: 75px; width: 400px">
</HeaderTemplate>
<ItemTemplate>
<label><%# Eval("DeptName") %></label>
<input type="checkbox" name="chkSelect" value='<%# Eval("DeptGroup") %>' group='<%# Eval("DeptGroup") %>'/>
<br />
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater ID="myRepeater1" runat="server">
<HeaderTemplate>
<table style="height: 75px; width: 400px">
</HeaderTemplate>
<ItemTemplate>
<label><%# Eval("EmpName") %></label>
<input type="checkbox" name="chkSelect1" value='<%# Eval("EmpGroup") %>' group='<%# Eval("EmpGroup") %>'/>
<br />
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>