Javascript 当用户没有';t在下拉菜单上选择一个选项
我制作了一个下拉菜单供用户选择,日期如下 如何提醒用户他们没有选择日期?月、日和年是Javascript 当用户没有';t在下拉菜单上选择一个选项,javascript,jquery,html,Javascript,Jquery,Html,我制作了一个下拉菜单供用户选择,日期如下 如何提醒用户他们没有选择日期?月、日和年是元素中的选项 if(document.forms[0].checkindate.selectedIndex == 1) { window.alert("You must select a date."); return false; } 我这样做了,但它不起作用。给定选择值为空,并使用html进行验证,如所需 <select required> <option valu
元素中的选项
if(document.forms[0].checkindate.selectedIndex == 1) {
window.alert("You must select a date.");
return false;
}
我这样做了,但它不起作用。给定选择值为空,并使用html进行验证,如所需
<select required>
<option value="">Month</option>
<option vslue="1">1</option>
</select>
<select required>
<option value="">Day</option>
<option vslue="1">1</option>
</select>
<select required>
<option value="">Year</option>
<option vslue="2016">2016</option>
</select>
月
1.
白天
1.
年
2016
或者使用Javascript或jQuery验证
<html>
<head>
<title>Form</title>
<script type="text/javascript">
function validate()
{
if(document.getElementById("city").value=="-1")
{
alert('Please select a city');
return false;
}
else if (document.getElementById('day').value=='')
{
alert('Please provide date for D.O.B');
document.getElementById("day").style.borderColor="red";
document.getElementById("day").style.backgroundColor="yellow";
document.getElementById("day").style.borderWidth=2;
return false;
}
else if (document.getElementById('month').value=='')
{
alert('Please provide month for D.O.B');
document.getElementById("month").style.borderColor="red";
document.getElementById("month").style.backgroundColor="yellow";
document.getElementById("month").style.borderWidth=2;
return false;
}
else if (document.getElementById('year').value=='')
{
alert('Please provide year for D.O.B');
document.getElementById("year").style.borderColor="red";
document.getElementById("year").style.backgroundColor="yellow";
document.getElementById("year").style.borderWidth=2;
return false;
}
}
</script>
<body>
<form >
City : <select id="city">
<option value="-1">-~Select One~-</option>
<option>City 1</option>
<option>City 2</option>
<option>City 3</option>
<option>City 4</option>
<option>City 5</option>
</select>
<br>
Date of Birth:
Day
<input type="number" name="day" id='day' min="1" max="31" value="" required>
Month
<input type="number" name="month" min="1" id='month' max="12" value="" required>
Year
<input type="number" name="year" min="1950" id='year' max="2020" value="" required>
<input type="submit" value="Login" onclick="return(validate());"
</form>
</body>
</html>
形式
函数验证()
{
if(document.getElementById(“city”).value==“-1”)
{
警报(“请选择一个城市”);
返回false;
}
else if(document.getElementById('day')。值=“”)
{
警报(“请提供交货日期”);
document.getElementById(“day”).style.borderColor=“红色”;
document.getElementById(“day”).style.backgroundColor=“黄色”;
document.getElementById(“day”).style.borderWidth=2;
返回false;
}
else if(document.getElementById('month')。值=“”)
{
警报(“请提供D.O.B的月份”);
document.getElementById(“月”).style.borderColor=“红色”;
document.getElementById(“月”).style.backgroundColor=“黄色”;
document.getElementById(“月”).style.borderWidth=2;
返回false;
}
else if(document.getElementById('year')。值=“”)
{
警报(“请提供D.O.B的年份”);
document.getElementById(“年”).style.borderColor=“红色”;
document.getElementById(“年”).style.backgroundColor=“黄色”;
document.getElementById(“年”).style.borderWidth=2;
返回false;
}
}
城市:
-~z~选一个~-
城市1
城市2
城市3
城市4
城市5
出生日期:
白天
月
年
您可以使用.each()
函数检查用户是否未选择任何下拉列表
$('select').each(function () {
if (this.value)
alert('value selected');
else
alert('no selection');
});
你的HTML在这里也会很有帮助。你能提供sjfiddle或plunker链接吗。所以我们可以尝试它在所有浏览器中都能发挥最大的作用,我认为一些非常低版本的浏览器是行不通的。如果我们也关注这一点,我们就必须使用Javascript或jQuery验证