<div class='input1'>
<select name="gender" class="gender" id="gender">
<option value="">Gender</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
JS
reg_gender = /(male|female)/;
if(form.gender.value == "" && (!reg_gender.test(form.gender.value)) ) {
alert("Please select gender!");
form.gender.focus();
return false;
}
EDITTED
答案 0 :(得分:0)
对 HTML 进行简单的更改:
<div class='input1'>
<select id="gender" name="gender" class="gender">
<option value="">Gender</option> <!-- make change value="" -->
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
还在 javascript 中添加一些代码:
reg_gender = /(male|female)/;
if(form.gender.value == "" && (!reg_gender.test(form.gender.value)) ) {
alert("Please select gender!");
form.gender.focus();
return false;
}
此表达式/(male|female)/
检查值是male
还是female