-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path年月日级联test1.html
77 lines (69 loc) · 2.05 KB
/
年月日级联test1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
</head>
<body>
<form id="form1" name="form1" method="post" action="">
请选择日期:
<select name="year" id="year">
<option value="0">--</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
</select>年
<select name="month" id="month">
<option value="0">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>月
<select name="day" id="day">
<option value="0">--</option>
</select>日
</form>
<script type="text/javascript">
//添加事件兼容方法
var addEvent = document.addEventListener ?
function (elem, type, listener, useCapture) {
elem.addEventListener(type, listener, useCapture);
} :
function (elem, type, listener, useCapture) {
elem.attachEvent('on' + type, listener);
};
var year = document.getElementById('year');
var month = document.getElementById('month');
var day = document.getElementById('day');
//初始化天
function initDate(yearVal, monthVal) {
day.options.length = 0;
day.add(new Option('--', '0'));
if (yearVal > 0 && monthVal > 0) {
var date0 = new Date(yearVal, monthVal, 0);
var lastDay = date0.getDate();
for (var i = 1; i <= lastDay; i++) {
day.add(new Option(i, i));
}
}
}
//年改变事件
addEvent(year, 'change', function () {
month.options[0].selected = true;
day.options[0].selected = true;
});
//月改变事件
addEvent(month, 'change', function () {
initDate(year.value, month.value);
})
</script>
</body>
</html>