我尝试验证登录脚本,其他所有操作都已完成,但我不知道如何进行下拉菜单。我如何验证它,以确保通过JavaScript确定选择默认选项以外的选项?
所有其他验证都是通过单独的.js表完成的,所以我不确定document.getElementById是否有效,或者我只是犯了愚蠢的错误?
HTML(表单ID为" registration_form"):
<select class="signup_input_styleV2" id="location" name="location">
<option value="false" disabled selected>Region</option>
<option value="Europe">Europe</option>
<option value="North America">North America</option>
<option value="South America">South America</option>
<option value"Oceania">Oceania</option>
<option value"Asia">Asia</option>
<option value"Africa">Africa</option>
<option value"Antarctica">Antarctica</option>
</select>
答案 0 :(得分:1)
if ( document.getElementsByName('location')[0].value == 'false' ){
alert('Select something !');
}
这可能就足够了。
答案 1 :(得分:0)
是的,您可以使用getElementById
function validate(){
var val = document.getElementById("location").value
console.log(val);
}
&#13;
<select class="signup_input_styleV2" id="location" name="location">
<option value="false" disabled selected>Region</option>
<option value="Europe">Europe</option>
<option value="North America">North America</option>
<option value="South America">South America</option>
<option value"Oceania">Oceania</option>
<option value"Asia">Asia</option>
<option value"Africa">Africa</option>
<option value"Antarctica">Antarctica</option>
</select>
<input type="submit" onClick="return validate();">
&#13;