JAVASCRIPT引用INPUT值问题

有两组INPUT值,A01,A02,如下
<input name="A01"  value="FD"  type="radio">
<input name="A01"  value="ERERER"  type="radio">
<input name="A01"  value="HGHGHGH"  type="radio">


<input name="A02"  value="FD"  type="radio">
<input name="A02"  value="ERERER"  type="radio">
<input name="A02"  value="HGHGHGH"  type="radio">


两组值中有一个属性为CHECKED, 就返回TRUE,否则返回FALSE

用JAVASCRIPT怎么写呢?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<title>无标题文档</title>
<script>
function checkRadio(){
    var flag = false;
    var radios = document.getElementsByName("A01");
    for(var i = 0; i < radios.length && !flag; i++){
        flag = flag || radios[i].checked;
    }

    radios = document.getElementsByName("A02");
    for(var i = 0; i < radios.length && !flag; i++){
        flag = flag || radios[i].checked;
    }
    return flag;
}
</script>
</head>

<body>
<form>
<input name="A01"  value="FD"  type="radio"/> 
<input name="A01"  value="ERERER"  type="radio"/> 
<input name="A01"  value="HGHGHGH"  type="radio"/> 


<input name="A02"  value="FD"  type="radio"/> 
<input name="A02"  value="ERERER"  type="radio"/> 
<input name="A02"  value="HGHGHGH"  type="radio"/>

<input type="button" value="验证" onclick="alert(checkRadio())"/>
<input type="reset" value="清空">
</form>
</body>
</html>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb_23-12" />
<title>无标题文档</title>
<script>
function chkRadio(n)
{//检查一项
    var o = document.getElementsByName(n);
    for(var i = 0;i < o.length;i++) if(o[i].checked) return true
    return false;
}

function withTow()
{//合并两项
    alert(chkRadio("A01") && chkRadio("A02"));
}
</script>
</head>

<body>
<input name="A01"  value="FD"  type="radio"> 
<input name="A01"  value="ERERER"  type="radio"> 
<input name="A01"  value="HGHGHGH"  type="radio"> 
<input type="button" value="检测A01" onclick="alert(chkRadio('A01'))"> 

<input name="A02"  value="FD"  type="radio"> 
<input name="A02"  value="ERERER"  type="radio"> 
<input name="A02"  value="HGHGHGH"  type="radio"> 
<input type="button" value="检测A02" onclick="alert(chkRadio('A02'))"> <br><br>

<input type="button" value="检测两项" onclick="withTow()"> 

</body>
</html>

你可能感兴趣的:(JavaScript,java,html,XHTML)