注册 登录
编程论坛 ASP技术论坛

不能检测到单选按钮是否被选中

dillon_dai 发布于 2007-10-02 22:44, 437 次点击
<script language="javascript">
function yanzheng()
{
if (document.tijiao.user_name.value=="")
{
window.alert("请填写姓名");
document.tijiao.user_name.focus();
return (false);
}
if (document.tijiao.radio.value=="")
{
window.alert("请选择性别");
document.tijiao.radio.focus();
return (false);
}
if (document.tijiao.age.value=="")
{
window.alert("请输入年龄");
document.tijiao.age.focus();
return (false);
}
}
</script>
<form id="form1" name="tijiao" method="post" action="user.asp?da=da1" onsubmit="return yanzheng();">
<label>姓名:
<input type="text" name="user_name" />
</label>
<label><br />
性别: </label>

<label>
<input type="radio" name="xingbie" value="男" />

<input type="radio" name="xingbie" value="女" />

<br />
年龄:
<input name="age" type="text" id="age" />
</label>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
<label>
<input type="reset" name="Submit2" value="重置" />
</label>
</p>
</form>
1 回复
#2
madpbpl2007-10-02 23:37

<script language="javascript">
function yanzheng()
{
if (document.tijiao.user_name.value=="")
{
window.alert("请填写姓名");
document.tijiao.user_name.focus();
return (false);
}
var check=true;
for(i=0;i<document.tijiao.xingbie.length;i++){
if(document.tijiao.xingbie[i].checked){
check=false;
break;
}
}
if (check)
{
window.alert("请选择性别");
document.tijiao.xingbie[0].focus();
return (false);
}
if (document.tijiao.age.value=="")
{
window.alert("请输入年龄");
document.tijiao.age.focus();
return (false);
}
}
</script>
<form id="form1" name="tijiao" method="post" action="user.asp?da=da1" onsubmit="return yanzheng();">
<label>姓名:
<input type="text" name="user_name" />
</label>
<label><br />
性别: </label>

<label>
<input type="radio" name="xingbie" value="男" />

<input type="radio" name="xingbie" value="女" />

<br />
年龄:
<input name="age" type="text" id="age" />
</label>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
<label>
<input type="reset" name="Submit2" value="重置" />
</label>
</p>
</form>
document.表单名.字段名(不是字段类型).value,
这个楼上要注意的

1