首页 > 代码库 > 年月日 日期选择问题

年月日 日期选择问题

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            div
            {
                height: 300px;
                width: 300px;
                margin: 0px auto;
            }
        </style>
    </head>
    <body>
        <div>
         <select id="year" onChange="a()">
               <option value=http://www.mamicode.com/"0" selected></option>
              <!--<option value=http://www.mamicode.com/"1986">1986</option>
              <option value=http://www.mamicode.com/"1987">1987</option> 
              <option value=http://www.mamicode.com/"1988">1988</option>
              <option value=http://www.mamicode.com/"1989">1989</option>
              <option value=http://www.mamicode.com/"1990">1990</option>
              <option value=http://www.mamicode.com/"1991">1991</option>
              <option value=http://www.mamicode.com/"1992">1992</option>
              <option value=http://www.mamicode.com/"1993">1993</option>
              <option value=http://www.mamicode.com/"1994">1994</option>
              <option value=http://www.mamicode.com/"1995">1995</option>
              <option value=http://www.mamicode.com/"1996">1996</option>
              <option value=http://www.mamicode.com/"1997">1997</option>
              <option value=http://www.mamicode.com/"1998">1998</option>
              <option value=http://www.mamicode.com/"1999">1999</option>
              <option value=http://www.mamicode.com/"2000">2000</option>
              <option value=http://www.mamicode.com/"2001">2001</option>
              <option value=http://www.mamicode.com/"2002">2002</option>
              <option value=http://www.mamicode.com/"2003">2003</option>
              <option value=http://www.mamicode.com/"2004">2004</option>
              <option value=http://www.mamicode.com/"2005">2005</option>
              <option value=http://www.mamicode.com/"2006">2006</option>
              <option value=http://www.mamicode.com/"2007">2007</option>
              <option value=http://www.mamicode.com/"2008">2008</option>
              <option value=http://www.mamicode.com/"2009">2009</option>
              <option value=http://www.mamicode.com/"2010">2010</option>
              <option value=http://www.mamicode.com/"2011">2011</option>
              <option value=http://www.mamicode.com/"2012">2012</option>
              <option value=http://www.mamicode.com/"2013">2013</option>
              <option value=http://www.mamicode.com/"2014">2014</option>
              <option value=http://www.mamicode.com/"2015">2015</option>
              <option value=http://www.mamicode.com/"2016">2016</option>
              <option value=http://www.mamicode.com/"2017">2017</option>-->
            </select><select id="month" onChange="b()">   
              <option value=http://www.mamicode.com/"0"></option>
              <!--<option value=http://www.mamicode.com/"1">1</option>
              <option value=http://www.mamicode.com/"2">2</option>
              <option value=http://www.mamicode.com/"3">3</option>
              <option value=http://www.mamicode.com/"4">4</option>
              <option value=http://www.mamicode.com/"5">5</option>
              <option value=http://www.mamicode.com/"6">6</option>
              <option value=http://www.mamicode.com/"7">7</option>
              <option value=http://www.mamicode.com/"8">8</option>
              <option value=http://www.mamicode.com/"9">9</option>
              <option value=http://www.mamicode.com/"10">10</option>
              <option value=http://www.mamicode.com/"11">11</option>
              <option value=http://www.mamicode.com/"12">12</option>-->
            </select><select id="day">                    
              <!--<option value=http://www.mamicode.com/"1" selected>1</option>
              <option value=http://www.mamicode.com/"2">2</option>
              <option value=http://www.mamicode.com/"3">3</option>
              <option value=http://www.mamicode.com/"4">4</option>
              <option value=http://www.mamicode.com/"5">5</option>
              <option value=http://www.mamicode.com/"6">6</option>
              <option value=http://www.mamicode.com/"7">7</option>
              <option value=http://www.mamicode.com/"8">8</option>
              <option value=http://www.mamicode.com/"9">9</option>
              <option value=http://www.mamicode.com/"10">10</option>
              <option value=http://www.mamicode.com/"11">11</option>
              <option value=http://www.mamicode.com/"12">12</option>
              <option value=http://www.mamicode.com/"13">13</option>
              <option value=http://www.mamicode.com/"14">14</option>
              <option value=http://www.mamicode.com/"15">15</option>
              <option value=http://www.mamicode.com/"16">16</option>
              <option value=http://www.mamicode.com/"17">17</option>
              <option value=http://www.mamicode.com/"18">18</option>
              <option value=http://www.mamicode.com/"19">19</option>
              <option value=http://www.mamicode.com/"20">20</option>
              <option value=http://www.mamicode.com/"21">21</option>
              <option value=http://www.mamicode.com/"22">22</option>
              <option value=http://www.mamicode.com/"23">23</option>
              <option value=http://www.mamicode.com/"24">24</option>
              <option value=http://www.mamicode.com/"25">25</option>
              <option value=http://www.mamicode.com/"26">26</option>
              <option value=http://www.mamicode.com/"27">27</option>
              <option value=http://www.mamicode.com/"28">28</option>-->
            </select></div>
    </body>
</html>
<script>
    
    var runnian;
    var opt1=Array();
    var opt2=Array();
    var year = document.getElementById("year");
    for(var k=1980;k<=2017;k++)
            {  
                opt1[k] = document.createElement(option);
                year.value = k;
                opt1[k].innerText = k;
                year.appendChild(opt1[k]);
            }
    function a()
    {
        
     var year = document.getElementById("year");
     var month = document.getElementById("month")
     month.options.length=1;    
     var day =document.getElementById(day);
     day.options.length=0;
      
     if((year.value%4==0&&year.value%100!=0)||year.value%400==0)
     {
         runnian=true;
     }
     else
     {
         runnian=false;
     }
     for(var j=1;j<=12;j++)
            {  
                opt1[j] = document.createElement(option);
                month.value = j;
                opt1[j].innerText = j;
                month.appendChild(opt1[j]);
            }
    }

以上实现的是年月日 日期时间选择(自己的版本)

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
	
		<select id=‘year‘ onchange="addDay()"></select>
		<select id="month" onchange="addDay()"></select>
		<select id="date"></select>
	</body>

</html>
<script>

	var year_slt = document.getElementById(‘year‘);
	var month_slt = document.getElementById(‘month‘);
	var day_slt = document.getElementById(‘date‘);
	var now = new Date();
	now_year = now.getFullYear();

	for(var i = now_year; i >= now_year - 70; i--) {
		var opt_year = document.createElement(‘option‘);
		opt_year.value = http://www.mamicode.com/i;>

  以上是老师的版本,相较有多处更加简洁的写法,值得以后借鉴

年月日 日期选择问题