如何将年份从旧值打印到当前值?

时间:2014-05-28 16:06:31

标签: javascript

我正在尝试使用此代码,但我不明白它的含义,

 <script type="text/javascript">

/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(dayfield, monthfield, yearfield){
var today=new Date()
var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i, i+1)
***dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day***
for (var m=0; m<12; m++)

 monthfield.options[m]=new Option(monthtext[m], monthtext[m])

monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear=today.getFullYear()
for (var y=0; y<20; 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
}

他们已经将这种代码用于所有三个值,日期,月份和年份。每个参数给出的值是什么,为什么?

我还想打印从较旧年份到当年的年份,我尝试按照以下方式修改它,但即使我打开下拉列表,我也会在当年进行修改。如何从列表中删除当前年份?

var thisyear=today.getFullYear() //get the current year in four digit number
    var startyear=1990
    var x= thisyear - startyear
    for (var y=0; y<x-1; y++){ 
        //yearfield.options[y]=new Option(thisyear, thisyear)
        yearfield.options[y]=new Option(startyear, thisyear)
        startyear+=1
    }

1 个答案:

答案 0 :(得分:0)

更改

var thisyear=today.getFullYear()

var thisyear=2005

(比如前一年是2005年)。然后,改变

yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true)

yearfield.options[today.getFullYear()-2005]=new Option(today.getFullYear(), today.getFullYear(), true, true)