jQuery元素也可以调用each方法,只是对$.each的简化调用。return false;终止循环


示例: 页面中有多个兴趣爱好,通过复选框设置,选中某个复选框,下面即可显示选中的个数及内容

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"><title>jQuery测试</title><scripttype="text/javascript"src="js/jquery-1.12.3.js"></script><scripttype="text/javascript">$(function(){$('#dvinput[type=checkbox]').click(function(){//varcks=$('#dvinput[type=checkbox]:checked');varcks=$('#dv:checkbox:checked');//简写varlen=cks.length;//记录个数vararr=[];cks.each(function(k,v){arr[arr.length]=$(v).val();//记录选中的value值});if(len==0){$('#result').text("");}else{$('#result').text('共选择了'+len+'个,分别是:'+arr);}});});</script></head><body><divid="dv"><label>爱好:</label><inputtype="checkbox"name="hobby"value="吃饭"id="cf"/><labelfor="cf">吃饭</label><inputtype="checkbox"name="hobby"value="睡觉"id="sj"/><labelfor="sj">睡觉</label><inputtype="checkbox"name="hobby"value="读书"id="ds"/><labelfor="ds">读书</label><inputtype="checkbox"name="hobby"value="运动"id="yd"/><labelfor="yd">运动</label><inputtype="checkbox"name="hobby"value="篮球"id="lq"/><labelfor="lq">篮球</label><inputtype="checkbox"name="hobby"value="游泳"id="yy"/><labelfor="yy">游泳</label></div><pid="result"></p></body></html>

效果图