网络编程
位置:首页>> 网络编程>> JavaScript>> js判断复选框是否选中及选中个数的实现代码

js判断复选框是否选中及选中个数的实现代码

作者:jingxian  发布时间:2024-04-22 22:17:03 

标签: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>
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com