JQuery练习——年月日级联查询

HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
年:<select id="selYear"><option value="-1">-年-</option></select>  
月:<select id="selMonth"><option value="-1">-月-</option></select>  
日:<select id="selDate"><option value="-1">-日-</option></select>
</body>
</html>

JavaScript代码:

<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
    var $selYear = $("#selYear");
    var $selMonth = $("#selMonth");
    var $selDate = $("#selDate");

    var yearList = [1988, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011];
    var monthList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    var dateList = new Array();
    dateList[-1] = [];
    dateList[0] = [29, 30, 31];
    dateList[1] = [29];
    dateList[2] = [29, 30];
    dateList[3] = [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];
    $(function () {
        //添加可选年份
        $(yearList).each(function (index, item) {
            $selYear.append("<option value='" + item + "'>" + item + "</option>");
        });

        //选择年份
        $selYear.change(function () {
            $selMonth.attr("options").length = 1; //或者$selMonth.get(0).options.length = 1;
            $selDate.attr("options").length = 1;
            if ($(this).val() != "-1") {
                $(monthList).each(function (index, item) {
                    $selMonth.append("<option value='" + item + "'>" + item + "</option>");
                });
            }
        });

        $selMonth.change(function () {
            $selDate.attr("options").length = 1;
            var month_temp = parseInt($(this).val());//获取选择的月份
            if (month_temp != -1) {
                //每月至少28天  
                $(dateList[3]).each(function (index, item) {
                    $selDate.append("<option value='" + item + "'>" + item + "</option>");
                });

                if (month_temp == 2) { //如果是2月份,则判断是平年还是闰年  
                    var year_temp = parseInt($selYear.val());//获取选择的年份
                    if ((year_temp % 4 == 0 && year_temp % 100 != 0) || (year_temp % 400 == 0)) { //闰年,2月29天  
                        $(dateList[1]).each(function (index, item) {
                            $selDate.append("<option value='" + item + "'>" + item + "</option>");
                        });
                    }
                } else { //其他月份无需判断是平年还是闰年                      
                    switch (month_temp) { //判断月大还是月小  
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            $(dateList[0]).each(function (index, item) {
                                $selDate.append("<option value='" + item + "'>" + item + "</option>");
                            });
                            break;
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            $(dateList[2]).each(function (index, item) {
                                $selDate.append("<option value='" + item + "'>" + item + "</option>");
                            });
                            break;
                    }
                }
            }
        });
    });
</script>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值