当前位置:编程学习 > JS >>

js判断复选框是否选中及选中个数

用js判断复选框是否选中及选中个数,以前经常有朋友提到复选框的选中问题,这段代码可以帮你解决这个问题,而且它还可以判断你选择了多少个复选框,多选则弹出提示。
答案:<HTML>
<head>
<title>判断复选框是否选中及选中个数</title>
<SCRIPT LANGUAGE="JavaScript">
function countChoices(obj) {
max = 2;
box1 = obj.form.box1.checked;
box2 = obj.form.box2.checked;
box3 = obj.form.box3.checked;
count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0);
if (count > max) {
alert("对不起,你只能选择" + max + "个!");
obj.checked = false;
   }
}
//-->
</script>
<body>
      <form name="form">
       你喜欢的语言是(最多两个): <p>
<input type=checkbox name=box1 onClick="countChoices(this)">ASP.NET<p> 
<input type=checkbox name=box2 onClick="countChoices(this)">JAVA<p> 
<input type=checkbox name=box3 onClick="countChoices(this)">C#<p> 
      </form>
</body>
</html>

上一个:可上移下移的下拉选择框
下一个:自动将文本框的值复制到另一个文本框

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,