0
我有以下JavaScript代碼,它在下拉菜單中顯示當天,月份和年份。代碼如下:顯示日期,月份和年份代替當前的日期,月份和年份
var monthtext=['January','February','March','April','May','June','July','August','September','October','November','December'];
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+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 < 100; 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 type="text/javascript">
//populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)
window.onload=function(){
populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
}
</script>
<select id="daydropdown" name="D-DOB"></select>
<select id="monthdropdown" name="M-DOB"></select>
<select id="yeardropdown" name="Y-DOB"></select>
我想顯示「選擇日」,「選擇月」,並在地方像電流值的「選擇年份」爲默認值:
<option value="">Select Day</option>
小提琴: https://jsfiddle.net/u2m243qf/1/
編輯:
由我自己解決:
我重構了我的代碼,並得到了一個更簡單的解決方案。新的小提琴: https://jsfiddle.net/u2m243qf/9/
對不起,我是不是在談論純html ...在以上javascript ... dayfield.options [today.getDate()] = new Option(today.getDate(),today.getDate(),true,true)// select today's天 – user3790186
那麼,在普通的JS: dayfield.value =「選擇日期」; – Paul
Sorry..it將不會工作。 – user3790186