注册时选择出生年月日的html+JS

<table>
<tr>
    <td width="23%" align="right"><span style="color:#F30">*</span>出生日期:</td>
    <td>  <select id="year" onchange="changeday()" name="year"> <option value="">年</option></select><select id="month" name="month"  onchange="changeday()"><option value="">月</option> </select><select id="day" name="day" ><option value="">日</option> </select>
    <script type="text/javascript" language="javascript">
                            var curdate = new Date();
                            var year = document.getElementById("year");
                            var month = document.getElementById("month");
                            var day = document.getElementById("day");
                            //绑定年份月分的默认
                            function add() {
                                var curyear = curdate.getFullYear();
                                var minyear = curyear - 80;
                                var maxyear = curyear - 18;
        for (maxyear; maxyear >= minyear; maxyear = maxyear - 1) {
                                    year.options.add(new Option(maxyear, maxyear));
                                }
                                for (var mindex = 1; mindex <= 12; mindex++) {
                                    month.options.add(new Option(mindex, mindex));
                                }
                            }

                            //判断是否是闰年
                            function leapyear(intyear) {
                                var result = false;
                                if (((intyear % 400 == 0) && (intyear % 100 != 0)) || (intyear % 4 == 0)) {
                                    result = true;
                                }
                                else {
                                    result = false;
                                }
                                return result;
                            }
                            //绑定天数
                            function addday(maxday) {
                                day.options.length = 1;
                                for (var dindex = 1; dindex <= maxday; dindex++) {
                                    day.options.add(new Option(dindex, dindex));
                                }
                            }
                            function changeday() {
                                if (year.value == null || year.value == "") {
                                    alert("请先选择年份!");
                                    return false;
                                }
                                else {
                                    if (month.value == 1 || month.value == 3 || month.value == 5 || month.value == 7 || month.value == 8 || month.value == 10 || month.value == 12) {
                                        addday(31);
                                    }
                                    else {
                                        if (month.value == 4 || month.value == 6 || month.value == 9 || month.value == 11) {
                                            addday(30);
                                        }
                                        else {
                                            if (leapyear(year.value)) {
                                                addday(29);
                                            }
                                            else {
                                                addday(28);
                                            }
                                        }
                                    }
                                }
                            }
                            window.onload = add;
                        </script></td>
    <td style="color:#333">(注册后不可更改)</td>
  </tr>
</table>

本博客所有文章如无特别注明均为原创。作者:sysdee复制或转载请以超链接形式注明转自 成功志
原文地址《注册时选择出生年月日的html+JS
分享到:更多

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(5)

软件工程实验用到了
datrilla 4年前 (2015-12-27) 回复
一个页面上需要使用两次出生年月,复制代码更改了ID。发现第二个出生年月的  F_year.options.F_add(new Option(F_maxyear, F_maxyear));
这里的options出错。
@答题王 5年前 (2014-12-16) 回复
@@答题王:这样的话,这个代码也要搞多一份了,最好是把它搞成一个闭包类,用的时候 new 一下
sysdee 5年前 (2015-01-12) 回复
qq水电费
aa 5年前 (2014-07-31) 回复
很好。。收藏了
Kevin 6年前 (2014-04-24) 回复