下面这道javascript究竟该怎样做呢?为啥我做的不对?
完成foo()函数的内容,要求能够弹出对话框提示当前选中的是第几个单选框。
<html>
<body>
<script>
function foo() {
// 在此处添加代码
return false;
}
</script>
<body>
<form name= "form1 " >
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "submit "/>
</form>
</body>
</html>
------------------------------
我写的如下:
function foo()
{// 在此处添加代码
for(var i=0;i <radioGroup.value;i++)
//这里该怎样写呢?radioGroup没有count或则length之类的表示 "个数 "的属性呀.
{
alert( '当前选中的是第: '+radioGroup[i]+ '个单选框 ');
}
return false;
}
运行后,点击“提交查询按钮”按钮,只是在IE地址栏中多了一个?radioGroup=on:
http://localhost:2649/WebSite2/MainFrm.aspx?radioGroup=on,并不能提示当前选中的是第几个单选框呀?请帮我看一下该怎么办?谢谢了先!
------解决方案--------------------这样。复制过去就可以
<html>
<body>
<script language= "JavaScript ">
function foo()
{
for(var i=0;i <document.form1.radioGroup.length;i++)
{
if(document.form1.radioGroup[i].checked)
{
alert( "你选中的是第 "+i+ "个 ");
}
}
}
</script>
<body>
<form name= "form1 " >
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "radio " name= "radioGroup "/>
<input type= "submit " onclick= "foo() "/>
</form>
</body>
</html>
------解决方案--------------------function foo()
{// 在此处添加代码
for(var i=0;i <document.forms[0].radioGroup.length;i++)
//这里该怎样写呢?radioGroup没有count或则length之类的表示 "个数 "的属性呀.
{
if(document.forms[0].radioGroupradioGroup[i].checked==true)
{
alert( '当前选中的是第: '+(i+1)+ '个单选框 ');
break;
}
else
{
continue;
}
}
return false;
}