三级联动下拉选项(年月日选择)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>三级联动年月日</title>
        <meta name="keywords" content="关键字列表" />
        <meta name="description" content="网页描述" />
        <link rel="stylesheet" type="text/css" href="" />
        <script type="text/javascript" src="js/jquery-1.9.1.min.js" ></script>
        <style>
            body{padding: 0;margin: 0;}
            .content{width: 1000px;margin: 50px auto;padding: 0;}
        </style>
    </head>
    <body>
        <div class="content">
            <select id="year">
                <option value="0">--请选择--</option>
            </select><select id="month">
                <option value="0">--请选择--</option>
            </select><select id="day">
                <option value="0">--请选择--</option>
            </select></div>
        <script>
            (function(){
                function selectFun(eleId,start,end){                    
                    for(start;start<=end;start++){
                        eleId.add(new Option(start,start));
                    }
                }
                var startY=1970;
                var end=new Date().getFullYear();
                var year=document.getElementById("year");
                var month=document.getElementById("month");
                var day=document.getElementById("day")
                selectFun(year,startY,end);
                year.onchange=function(){
                    month.length=1;
                    day.length = 1;
                    if(this.value!=0){                        
                        selectFun(month,1,12);
                    }
                }
                month.onchange=function(){
                    day.length = 1;
                    var value=this.value;
                    if(value!=0){
                        if(value==2){
                            console.log('2----');
                            if(ifRunYear(year.value)){
                                selectFun(day,1,29);
                            }else{
                                selectFun(day,1,28);
                            }
                        }else if(value==4||value==6||value==9||value==11){
                            console.log('30----');
                            selectFun(day,1,30);
                        }else{
                            console.log('31----');
                            selectFun(day,1,31);
                        }                        
                    }                    
                }
                function ifRunYear(year){
                    return (year% 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
                }
            })();
        </script>
    </body>
</html>

 

posted @ 2016-05-03 10:27 U_can 阅读(...) 评论(...) 编辑 收藏