js验证web单选框
<script language= "JavaScript ">
function FormCheck()
{
if(!document.form1.a.checked)
{
alert( "222 ");
return false;
}
}
</script>
<FORM name= "form1 " action= "register.asp " method=post >
<input type= "radio " name= "a " value= "d ">
<input type= "radio " name= "a " value= "c ">
<input type= "submit " name= "Submit " value= "sub ">
</form>
我用上面代码强制用户选择单选框,可即便选择了单选框还要求用户选,请问如何解决?
================================================
不是这样的,document.form1.a取到的是一个数组,不是一个radioBox,所以会出错,使用下面这个方法来取
<script language= "javascript ">
/**
*get the value of a group of radiobox
*@param radioBox the object of radioBox
*@return the value of the radioBox checked or false for none checked
*@example var value = getRadioValue(this.form1.radioGroup);
*/
function getRadioValue(radioBox){
try{
if(radioBox == null){
return false;
}
var length = radioBox.length;
if(length == null){
if(radioBox.checked)
return radioBox.value;
else
return false;
}else{
for(i = 0; i < length; i++) {
if(radioBox.checked)
return radioBox.value;
}
return false;
}
}catch(e) {
return false;
}
}
function FormCheck(){
if(!getRadioValue(document.form1.a)){
alert( "222 ");
return false;
}
}
</script>
{:2_25:}
页:
[1]