<div class="codetitle"> <span><a style="CURSOR: pointer" data="61120" class="copybut" id="copybut61120" onclick="doCopy('code61120')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code61120"> <br> <br> <br> <br><meta charset="utf-8"> <br><title>jquery test</title> <br><script src="jquery-1.11.1.min.js"></script> <br> <br><br> <br><input type="checkbox" name="check" value="one">one<br> <br><input type="checkbox" name="check" value="two">two<br> <br><input type="checkbox" name="check" value="three">three<br> <br><input type="checkbox" name="check" value="four">four<br> <br><input type="checkbox" name="check" value="five">five<br> <br><input type="checkbox" name="check" value="six">six<br> <br><input type="checkbox" name="check" value="seven">seven<br> <br><button name="sub">提交</button> <br><script type="text/javascript"> <BR>$("button[name=sub]").click(function(){ <BR>var len = $("input:checkbox:checked").length; <BR>alert("你一共选中了"+len+"个复选框"); <BR>}) <BR></script> <br> <br> <br> </div> <br>使用选择器得到所有被勾选的复选框元素的集合,然后通过判断元素的个数来得到用户勾选的个数。 <br><br>有的时候,我们还对用户勾选复选框的个数做了限制,假设只能勾选三个,相应的代码是这样的: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="52834" class="copybut" id="copybut52834" onclick="doCopy('code52834')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code52834"> <br> <br> <br> <br><meta charset="utf-8"> <br><title>jquery test</title> <br><script src="jquery-1.11.1.min.js"></script> <br> <br><br> <br><input type="checkbox" name="check" value="one">one<br> <br><input type="checkbox" name="check" value="two">two<br> <br><input type="checkbox" name="check" value="three">three<br> <br><input type="checkbox" name="check" value="four">four<br> <br><input type="checkbox" name="check" value="five">five<br> <br><input type="checkbox" name="check" value="six">six<br> <br><input type="checkbox" name="check" value="seven">seven<br> <br><script type="text/javascript"> <BR>$("input:checkbox").click(function(){ <BR>var len = $("input:checkbox:checked").length; <BR>if(len>3){ <BR>alert('亲,最多只能选三个哟~'); <BR>return false; //另刚才勾选的取消 <BR>} <BR>}) <BR></script> <br> <br> <br> </div>