JavaScript,判断,表单,多选框,checkbox,选中个数本文实例讲述了JavaScript判断表单中多选框checkbox选中个数的方法。分享给大家供大家参考。具体如下: 这里使用JavaScript检测并判断出表单中多选框的选中个数,也就是checkbox被选择了多少,在以前,这个问题经常被各大论坛问到,因为检测checkbox不像检测输入框那么简单,尤其是判断个数也经常会遇到,所以说觉得这个Js代码还是很有用的,大家有兴趣的再完善一下。 运行效果如下图所示: 
在线演示地址如下: http://demo.wanshiok.com/js/2015/js-checkbox-chk-num-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>检测表单多选框的选择个数</title><meta http-equiv="content-type" content="text/html;charset=gb2312"></head><body><script language="javascript"><!--function anyCheck(form) {var total = 0;var max = form.ckbox.length;for (var idx = 0; idx < max; idx++) {if (eval("document.playlist.ckbox[" + idx + "].checked") == true) { total += 1; }}alert("您选择了 " + total + " 个选项!");}//--></script><form method="post" name="playlist">1<input type="checkbox" name="ckbox" value="1">2<input type="checkbox" name="ckbox" value="2">3<input type="checkbox" name="ckbox" value="3">4<input type="checkbox" name="ckbox" value="4">5<input type="checkbox" name="ckbox" value="5">6<input type="checkbox" name="ckbox" value="6"><br><input type="button" value="检测选择个数" onClick="anyCheck(this.form)"></form></body></html> 希望本文所述对大家的javascript程序设计有所帮助。 JavaScript,判断,表单,多选框,checkbox,选中个数
|