<html>
<head>
<title>控制表单选择框个数</title>
</head>
<body>
<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>
<form name="form">
请最多选择2个项目:
<p>
<input type=checkbox name=box1 onClick="countChoices(this)">
游泳
<p>
<input type=checkbox name=box2 onClick="countChoices(this)">
散步
<p>
<input type=checkbox name=box3 onClick="countChoices(this)">
听音乐
<p>
</form>
</body>
</html>
posted on 2017-11-15 16:00  FAIRY_TALE  阅读(706)  评论(0编辑  收藏  举报