<form action="#" class="group" method="post" onsubmit="return checkforblank()">
<legend><span class="number">1</span>Departing & Arriving</legend>
<fielset class="col-sm-6">
<label for="FDestination">From</label>
<select name="Location" id="Location">
<option value="Please Select">Please Select</option>
<option value="Newport">Newport</option>
<option value="Mahdi">Mahdi</option>
<option value="Cardiff">Cardiff</option>
<option value="Cilo">Cilo is</option>
</select>
</fieldset>
</form>
<script>
function checkforblank(){
if (document.getElementsByID('Location').value== "Please Select") {
alert('Please enter first name');
document.getElementById('Location').style.borderColor = "red";
return false;
}
}
</script>
如何在用户从字段集中选择“请选择”选项时显示错误?如何使用JavaScript在HTML中验证“选择”标签
有一个在您的文章错字。 document.getElementsByID('Location')。value应该是document.getElementById('Location')。值 – Payson
@Payson我建议发布这个答案。另外,尝试使用无论您使用哪种浏览器来测试代码的开发工具。这将是一个伟大的资产,在节省时间和未来的挫折:) –
@LukeG谢谢。好的提示 – Payson