@@关于getElementsByName和"如何控制复选框选中数量"的探讨@@(恳切希望黄蓉老师能够参与)
1.我搜索到一帖,是关于getElementsByName的:
emu(ston) 说它返回答是对象集合,不是数组.
xinyunyishui(心云意水) 说返回数组.
请问到底是什么? 二者有区别吗?
2.关于这个问题,大家有没有好一点的算法:
9个复选框,最多可以选5个,超过5个时,让第六个被选中的复选框恢复为原来
没有选中的状态.
我能想到的就是possible_Y的方法:
<script>
function check(obj)
{
var j=0;
var el = f1.elements;
var count = el.length;
for (i=0;i<count;i++)
{
if (el[i].type=="checkbox" && el[i].checked==true)
j++;
}
if(j>5)
{
alert("最多只能選取5個");
obj.checked=false;
}
}
</script>
<form name="f1" action="" method="post">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
</form>
我现在有这么个想法:
不用在每个input中写onclick事件, 在form中定义onclick事件,然后用循环遍历每个
checkbox,利用一个计数变量统计选中的数量.但是有一个问题没有解决,就是如何得知哪个复选框是最后被选中的. 如果这个问题解决就可以使最后选中的复选框恢复.
希望大家能思考一下,谢谢!