首页 > 代码库 > js时间日期的三级联动

js时间日期的三级联动

aspx:

<div style="width:70px;float:left;">生日</div>
            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日

js:

<script>
  //年份变动的事件 document.getElementById("DropDownList1").onchange = function () { var mon = document.getElementById("DropDownList2"); var day = document.getElementById("DropDownList3"); if (mon.value =http://www.mamicode.com/= "2") { if (this.value % 4 == 0 && this.value % 100 != 0 || this.value % 400 == 0) { day.options.length = 0; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } else if (mon.value =http://www.mamicode.com/= "1" || mon.value =http://www.mamicode.com/= "3" || mon.value =http://www.mamicode.com/= "5" || mon.value =http://www.mamicode.com/= "7" || mon.value =http://www.mamicode.com/= "8" || mon.value =http://www.mamicode.com/= "10" || mon.value =http://www.mamicode.com/= "12") { day.options.length = 0; for (var i = 1; i <= 31; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i <= 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } }   //月变动的事件 document.getElementById("DropDownList2").onchange = function () { var day = document.getElementById("DropDownList3"); var year = document.getElementById("DropDownList1"); if (this.value =http://www.mamicode.com/= "2") { if (year.value % 4 == 0 && year.value % 100 != 0 || year.value % 400 == 0) { day.options.length = 0; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } else if (this.value =http://www.mamicode.com/= "1" || this.value =http://www.mamicode.com/= "3" || this.value =http://www.mamicode.com/= "5" || this.value =http://www.mamicode.com/= "7" || this.value =http://www.mamicode.com/= "8" || this.value =http://www.mamicode.com/= "10" || this.value =http://www.mamicode.com/= "12") { day.options.length = 0; for (var i = 1; i <= 31; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i <= 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } </script>

后台cs:

if (!IsPostBack)
        {
            for (int i = DateTime.Now.Year; i >= 1900; i--)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                DropDownList1.Items.Add(li);
            }
            for (int i = 1; i < 13; i++)
            {
                DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = 1; i < 31; i++)
            {
                DropDownList3.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        
        
        }

 

js时间日期的三级联动