日期:2014-05-18  浏览次数:20692 次

求教,关于radio的判断
<input   type= "radio "   name= " "   value= "1 ">
<input   type= "radio "   name= " "   value= "2 ">

这两个是结果集循环的   可以有N个
例:
<input   type= "radio "   name= "a "   value= "1 ">
<input   type= "radio "   name= "b "   value= "2 ">
<input   type= "radio "   name= "c "   value= "1 ">
<input   type= "radio "   name= "d "   value= "2 ">
<input   type= "radio "   name= "e "   value= "1 ">
<input   type= "radio "   name= "f "   value= "2 ">
..................
   
怎么判断   不能所有的都选择都是2     必须要有至少一个为1

请指教

------解决方案--------------------
<html>
<head>
<SCRIPT>
function check()
{
var flag1=false,flag2=false;
var radios = document.getElementsByTagName( "INPUT ");
for (var i = 0; i < radios.length; i++)
{
if (radios[i].type == "radio " && radios[i].checked)
{
if (radios[i].value == "1 ")
{
flag1 = true;
}
else if (radios[i].value == "2 ")
{
flag2 = true;
}
}
}
if (!flag1)
{
alert( "值为1的没有被选中 ");
}
if (!flag2)
{
alert( "值为2的没有被选中 ");
}
}
</SCRIPT>
</head>
<body onload= " ">
<form>
<input type= "radio " name= "a " value= "1 ">
<input type= "radio " name= "b " value= "2 ">
<input type= "radio " name= "c " value= "1 ">
<input type= "radio " name= "d " value= "2 ">
<input type= "radio " name= "e " value= "1 ">
<input type= "radio " name= "f " value= "2 ">
<input type= "button " value= "检验 " onclick= "check() ">
</form>
</body>
</html>