关于radio的问题?!

flylyke 2003-11-24 12:37:39
<script>
function checkradio(){
var a=document.getElementsByTagName("input");
for(i=0;i<a.length;i++){
//alert(a[i].type);
if(a[i].type=="radio"){
if(!a[i].checked){
alert("no");
break;
}
else {
alert("yes");
break;
}
}

}

}
</script>
<input type="radio" name="bb" value="win">
<input type="radio" name="bb" value="tie">
<input type="radio" name="bb" value="lost">

<br>
<input type="radio" name="aa" value="win">
<input type="radio" name="aa" value="tie">
<input type="radio" name="aa" value="lost"><br>
<input type="button" onclick="checkradio()">

当我全都中radio后还是会弹出"no"?为什么呢?
怎么写才能判断radio没有被选中?!
...全文
57 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
flylyke 2003-11-25
  • 打赏
  • 举报
回复
那我要是有N组radio这样岂不是要要判断很多次?!
gdjxlhc 2003-11-24
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<script>
function checkradio(){
var a=document.getElementsByTagName("input");
var aa=0;
var bb=0;
for(i=0;i<a.length;i++){
if(a[i].type=="radio" && a[i].name=="bb")
{
if(a[i].checked && bb!=1)
{
bb=1;
}
}
if(a[i].type=="radio" && a[i].name=="aa")
{
if(a[i].checked && aa!=1)
{
aa=1;
}
}
}
if(aa==1 && bb==1)
alert("yes");
else
alert("no");
}
</script>
<input type="radio" name="bb" value="win">
<input type="radio" name="bb" value="tie">
<input type="radio" name="bb" value="lost">

<br>
<input type="radio" name="aa" value="win">
<input type="radio" name="aa" value="tie">
<input type="radio" name="aa" value="lost"><br>
<input type="button" onclick="checkradio()">
</body>
</html>
rongwenfeng 2003-11-24
  • 打赏
  • 举报
回复
你的radio可以全部选中?????那就是系统有问题了
fzel_net 2003-11-24
  • 打赏
  • 举报
回复
agree with bencalie(Bencalie)
ajsword 2003-11-24
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<INPUT TYPE="radio" NAME="rad1"><br>
<INPUT TYPE="radio" NAME="rad2"><br>
<INPUT TYPE="radio" NAME="rad3"><br>
<INPUT TYPE="radio" NAME="rad4"><br>
<INPUT TYPE="radio" NAME="rad5"><br>
<INPUT TYPE="button" NAME="btn" onclick="clk()">
</BODY>
</HTML>

<SCRIPT LANGUAGE="JavaScript">
<!--
function clk() {
var bo_all_no = true;
var bo_all_yes = true;
var ar_radio = document.getElementsByTagName("INPUT");
for( var i=0; i<ar_radio.length; i++ ) {
if( ar_radio[i].type == "radio" ) {
if( ar_radio[i].checked == true ) {
bo_all_no = false;
} else {
bo_all_yes = false;
}
}
}
if( bo_all_no == true ) {
alert("全部没有选中");
}
if( bo_all_yes == true ) {
alert("全部选中");
}
}
//-->
</SCRIPT>
flylyke 2003-11-24
  • 打赏
  • 举报
回复
有一个没选的时候还是通过了?
bencalie 2003-11-24
  • 打赏
  • 举报
回复
<script>
function checkradio(){
var a=document.getElementsByTagName("input");
var choiceornot = false
for(i=0;i<a.length;i++){
//alert(a[i].type);
if(a[i].type=="radio"){
if(a[i].checked){
choiceornot = true
continue;
}
}
}
if(!choiceornot){
alert("no")
}
}
</script>
<input type="radio" name="bb" value="win">
<input type="radio" name="bb" value="tie">
<input type="radio" name="bb" value="lost">

<br>
<input type="radio" name="aa" value="win">
<input type="radio" name="aa" value="tie">
<input type="radio" name="aa" value="lost"><br>
<input type="button" onclick="checkradio()">

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧