我在下拉列表中尝试了下一个和前5年的html代码..我正确地获得了下5年..我如何获得前5年

请他给我密码
      
       

function populatedropdown(yearfield){
var today=new Date()
var yearfield=document.getElementById(yearfield)
var thisyear=today.getFullYear()
for (var y=0; y<5; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}

</script>
  </head>
  <body>
<form action="" name="someform">
<select id="yeardropdown">
</select>
</form>

<script type="text/javascript">

//populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)
window.onload=function(){
populatedropdown("yeardropdown")
}
</script>
  </body>
</html>

最佳答案

尝试这个



function populatedropdown(yearfield) {
  var today = new Date(),
      yearfield = document.getElementById(yearfield),
      beforeYears = 3
      afterYears = 5,
      thisyear = today.getFullYear();

  for (var year = thisyear-beforeYears, index = 0;
           index <= beforeYears + afterYears;
           index++, year++) {
    yearfield.options[index] = new Option(year, year);
  }
  yearfield.value = thisyear;
    //select today's year
}

//populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)
window.onload = function() {
  populatedropdown("yeardropdown")
}

<form action="" name="someform">
  <select id="yeardropdown">
  </select>
</form>

10-02 19:37