<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script src="../0622/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!--每选择一次年重置一下月和日-->
<select class="year" onchange="month(),day()"></select>
<!--每选择一次月重置一下日-->
<select class="month" onchange="day()"></select>
<select class="day"></select>
</body>
</html>
<script type="text/javascript">
$(function(){
year();
month();
day();
})
function year(){
var year = new Date().getFullYear();
var str = "";
for (var i = year-10;i<year+10;i++) {
str += "<option>"+i+"</option>";
}
$(".year").html(str);
}
function month(){
var year = new Date().getMonth();
var str = "";
for (var i = 1;i<13;i++) {
str += "<option>"+i+"</option>";
}
$(".month").html(str);
}
function day(){
// 选则日之前先获取年和月
var year_val = $(".year").val();
var month_val = $(".month").val();
var day_val = 30;
// 闰年的判断:普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);
if(month_val == 1 || month_val == 3 || month_val == 5 || month_val == 7 || month_val == 8 || month_val == 10 || month_val == 12){
day_val = 31;
}else if(month_val == 2){
if((year_val%4 == 0 && year_val%100 != 0)||year_val%400 == 0){
day_val = 29;
}else{
day_val = 28;
}
}
var year = new Date().getDate();
var str = "";
for (var i = 1;i<=day_val;i++) {
str += "<option>"+i+"</option>";
}
$(".day").html(str);
}
</script>