码迷,mamicode.com
首页 > 其他好文 > 详细

年月日三级联动

时间:2016-08-17 13:40:29      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

<select id="sel_year"></select>
<select id="sel_month"></select>
<select id="sel_day"></select>

 

 1 function dateLinkage() {
 2     var objYear = $("#sel_year"), objMonth = $("#sel_month"), objDay = $("#sel_day");
 3     var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
 4     var y = new Date().getFullYear();
 5     var str1 = "", str2 = "", str3 = "";
 6 
 7     //生成年
 8     for (var i = (y - 60) ; i < y; i++) {
 9         str1 += ‘<option value="‘ + i + ‘">‘ + i + ‘</option>‘;
10     }
11     objYear.html(str1);
12 
13     //生成月
14     var m = "";
15     for (var i = 1; i <= 12; i++) {
16         m = i < 10 ? ("0" + i) : i;
17         str2 += ‘<option value="‘ + m + ‘">‘ + m + ‘</option>‘;
18     }
19     objMonth.html(str2);
20 
21     //生成日
22     var d = "";
23     for (var i = 1; i <= 31; i++) {
24         d = i < 10 ? ("0" + i) : i;
25         str3 += ‘<option value="‘ + d + ‘">‘ + d + ‘</option>‘;
26     }
27     objDay.html(str3);
28 
29     //年份变化事件
30     objYear.change(function () {
31         var curm = parseInt(objMonth.val());
32         var num = MonHead[curm - 1];
33         if (IsLeapYear(this.value)) {
34             num = 29;
35         }
36         str3 = "";
37         for (var i = 1; i <= num; i++) {
38             d = i < 10 ? ("0" + i) : i;
39             str3 += ‘<option value="‘ + d + ‘">‘ + d + ‘</option>‘;
40         }
41         objDay.html(str3);
42     });
43 
44     //月份变化事件
45     objMonth.change(function () {
46         var curm = parseInt(this.value);
47         var num = MonHead[curm - 1];
48         if (IsLeapYear(objYear.val()) && curm == 2) {
49             num = 29;
50         }
51         str3 = "";
52         for (var i = 1; i <= num; i++) {
53             d = i < 10 ? ("0" + i) : i;
54             str3 += ‘<option value="‘ + d + ‘">‘ + d + ‘</option>‘;
55         }
56         objDay.html(str3);
57     });
58 
59     //判断是否是闰年
60     function IsLeapYear(year) {
61         return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0));
62     }
63 }

 

年月日三级联动

标签:

原文地址:http://www.cnblogs.com/shousiji/p/5779465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!