年月日三級連動メニュー

6639 ワード

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>        </title>
    <script type="text/javascript">

        //         
        function DateSelector(selYear, selMonth, selDay)
        {
            this.selYear = selYear;
            this.selMonth = selMonth;
            this.selDay = selDay;
            this.selYear.Group = this;
            this.selMonth.Group = this;
            //    、        onchange      
            if(window.document.all != null){
                //IE       
                this.selYear.attachEvent("onchange", DateSelector.Onchange);
                this.selMonth.attachEvent("onchange", DateSelector.Onchange);
            }
            else{
                // Firefox       
                this.selYear.addEventListener("change", DateSelector.Onchange, false);
                this.selMonth.addEventListener("change", DateSelector.Onchange, false);
            }
            if(arguments.length == 4) {

                //          4,         Date  
                this.InitSelector(arguments[3].getFullYear(), arguments[3].getMonth() + 1, arguments[3].getDate());

            } else if(arguments.length == 6) {

                 //          6,                 
                this.InitSelector(arguments[3], arguments[4], arguments[5]);

            } else {

                //         
                var dt = new Date();
                this.InitSelector(dt.getFullYear(), dt.getMonth() + 1, dt.getDate());
            }
        }

 

        //            
        DateSelector.prototype.MinYear = (new Date()).getFullYear();
        //            
        DateSelector.prototype.MaxYear = 2020;

 

        //      
        DateSelector.prototype.InitYearSelect = function() {
            //     OPION     select   
            for(var i = this.MinYear; i <= this.MaxYear; i++) {
                //     OPTION  
                var op = window.document.createElement("OPTION");
                //   OPTION    
                op.value = i;
                //   OPTION     
                op.innerHTML = i;
                //      select  
                this.selYear.appendChild(op);
            }
        }

        //      
        DateSelector.prototype.InitMonthSelect = function() {
            //     OPION     select   
            for(var i = 1; i < 13; i++)
            {
                //     OPTION  
                var op = window.document.createElement("OPTION");
                //   OPTION    
                op.value = i;
                //   OPTION     
                op.innerHTML = i;
                //      select  
                this.selMonth.appendChild(op);
            }
        }

        //               
        DateSelector.DaysInMonth = function(year, month) {
            var date = new Date(year, month, 0);
            return date.getDate();
        }

        //      
        DateSelector.prototype.InitDaySelect = function() {
            //   parseInt            
            var year = parseInt(this.selYear.value);
            var month = parseInt(this.selMonth.value);
            
            //        
            var daysInMonth = DateSelector.DaysInMonth(year, month);
            
            //        
            this.selDay.options.length = 0;
            //     OPION     select   
            for(var i = 1; i <= daysInMonth ; i++) {
                //     OPTION  
                var op = window.document.createElement("OPTION");
                //   OPTION    
                op.value = i;
                //   OPTION     
                op.innerHTML = i;
                //      select  
                this.selDay.appendChild(op);
            }
        }

        //        onchange     ,                

        //      Group     InitDaySelect         
        //   e event  
        DateSelector.Onchange = function(e) {
            var selector = window.document.all != null ? e.srcElement : e.target;
            selector.Group.InitDaySelect();
        }

        //              
        DateSelector.prototype.InitSelector = function(year, month, day) {
            //             ,        selYear selMonth     
            //     InitDaySelect             ,            
            this.selYear.options.length = 0;
            this.selMonth.options.length = 0;
            
            //     、 
            this.InitYearSelect();
            this.InitMonthSelect();
            
            //    、    
            this.selYear.selectedIndex = this.MinYear - year;
            this.selMonth.selectedIndex = month - 1;
            
            //      
            this.InitDaySelect();
            
            //        
            this.selDay.selectedIndex = day - 1;
        }
    </script>
</head>
<body>
    <form id="form1" action="">
        <select id="selYear"></select> 

        <select id="selMonth"></select> 

        <select id="selDay"></select> 
    </form>
</body>
</html>
<script type="text/javascript">
    var selYear = window.document.getElementById("selYear");
    var selMonth = window.document.getElementById("selMonth");
    var selDay = window.document.getElementById("selDay");
    var now = new Date();
    //        ,         
    new DateSelector(selYear,selMonth,selDay);
    //new DateSelector(selYear, selMonth ,selDay, now);
    //new DateSelector(selYear, selMonth ,selDay, now.getFullYear(), now.getMonth()+1, now.getDate());
</script>