I'm trying to have an error message/alert pop up when two checkboxes in the same row are selected. 我已经能够计算出复选框的总数,并且我知道如何计算当前选中了多少。我的每个复选框都有一个唯一的 ID。
<body>
<table id="tester">
<tr>
<td width="100" id="A1" ><label><input type="checkbox" name="A" value=1 id="A1" /> A1</label></td>
<td width="100" id="B1" ><label><input type="checkbox" name="B" value=1 id="B1" /> B1</label></td>
</tr>
<tr>
<td width="100" id="A2" ><label><input type="checkbox" name="A" value=1 id="A2" /> A2</label></td>
<td width="100" id="B2" ><label><input type="checkbox" name="B" value=1 id="B2" /> B2</label></td>
</tr>
</table>
<input id="clickMe" type="button" value="clickme" onclick="test();" />
</body>
我的 jquery 技能非常基础。这是我一直用来计算框数和选中框数的代码。
$(document).ready();
var len = $('input:checkbox').length;
console.log("There are "+len+" checkboxes");
var count2;
var test = function(){
var count2 = $("[type='checkbox']:checked").length;
console.log("There are "+count2+" checked checkboxes");
};
任何帮助都会很棒,谢谢!