我正在尝试验证 javascript 中的单选按钮和选项框。这是代码,但它不起作用... OprionBox 和 Radio 的 Javascript 代码:
function checkCountry()
{
if(document.form.country.selectedIndex=="")
{
alert("Please select country from the list");
return false;
}
return true
}
function checkGender()
{
if(!document.getElementsByName("sex")[0].checked && !document.getElementsByName("sex")[1].checked)
{
alert("Select Male/Female");
return false;
}
return true;
}
function validate()
{
checkCountry();
checkGender();
}
HTML 代码:
<form name="form" method="post" onSubmit="return validate()">
<select name="country">
<option value="select">(Please select a country)</option>
<option value="pk">Pakistan</option>
<option value="chn">China</option>
<option value="uk">United Kingdom</option>
<option value="usa">United States of America</option>
<option value="ir">Iran</option>
<option value="ma">Malaysia</option>
</select><br>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female<br></form>
请帮忙...