關於時間的操作(JavaScript版)——年月日三級級聯(預設依次顯示請選擇年、請選擇月和請選擇日)

來源:互聯網
上載者:User

        這篇部落格和前一篇部落格基本相同,只是顯示的預設值不同:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>年月日三級級聯(預設依次顯示請選擇年、請選擇月和請選擇日)</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"/><script type="text/javascript">function removeChilds(id){var childs = document.getElementById(id).childNodes;//這一行代碼和緊跟的下面的for迴圈用於清除原來日的下拉式清單的select中的對節點for(var i=childs.length-1;i>=0;i--) {document.getElementById(id).removeChild(childs[i]);      }}function setDay(){var yearToDate=document.getElementById("year").value;var monthToDate=document.getElementById("month").value;//alert(yearToDate+":"+monthToDate);var days=new Array(28,29,30,31);if(yearToDate==0||monthToDate==0){//如果是當前系統時間則設定預設的日var newOption = document.createElement("option");newOption.setAttribute("value",0);newOption.setAttribute("selected","selected");var textToNewOption=document.createTextNode("請選擇日");newOption.appendChild(textToNewOption);document.getElementById("day").appendChild(newOption);}else{if(monthToDate==1||monthToDate==3||monthToDate==5||monthToDate==7||monthToDate==8||monthToDate==10||monthToDate==12){removeChilds("day");for( i=1; i<=days[3]; i++ ){var newOption = document.createElement("option");newOption.setAttribute("value",i);var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);document.getElementById("day").appendChild(newOption);}}if(monthToDate==4||monthToDate==6||monthToDate==9||monthToDate==11){removeChilds("day");for( i=1; i<=days[2]; i++ ){var newOption = document.createElement("option");newOption.setAttribute("value",i);var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);document.getElementById("day").appendChild(newOption);}}if(monthToDate==2){removeChilds("day");if(yearToDate%400==0||yearToDate%100!=0&&yearToDate%4==0){//閏年for( i=1; i<=days[1]; i++ ){var newOption = document.createElement("option");newOption.setAttribute("value",i);var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);document.getElementById("day").appendChild(newOption);}}else{for( i=1; i<=days[0]; i++ ){var newOption = document.createElement("option");newOption.setAttribute("value",i);var textToNewOption=document.createTextNode(i);newOption.appendChild(textToNewOption);document.getElementById("day").appendChild(newOption);}}} }}function getMonth(){var m;document.write("<option value=0 selected=\"selected\">請選擇月</option>");for(m=1;m<=12;m++) {document.write("<option value="+m+">"+m+"</option>");}}function getYear(){var y;var date=new Date(); var fullYear=date.getFullYear();document.write("<option value=0 selected=\"selected\">請選擇年</option>");for(y=fullYear-60;y<=fullYear;y++){document.write("<option value="+y+" >"+y+"</option>");} }function checkDay(){var yearToDate=document.getElementById("year").value;var monthToDate=document.getElementById("month").value;if(yearToDate==0||monthToDate==0){alert("請先選擇年和日");}}</script></head><body><select name="year" id="year" onChange="setDay();"><script type="text/javascript">getYear();</script></select>年 <select name="month" id="month" onChange="setDay()"><script type="text/javascript">getMonth();</script></select>月 <select name="day" id="day" onclick="checkDay()"></select>日<script type="text/javascript">setDay();<!--起到初始化日的作用。--></script></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.