출처: http://november11tech.tistory.com/88 [Mr.november11] 년, 월,일 날짜 콤보박스 만들기 :: 사월은 봄이다.

<script type="text/javascript">


var monthtext=['1','2','3','4','5','6','7','8','9','10','11','12'];


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, 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


}


</script>


<form action="" name="someform">


<select id="daydropdown">


</select>


<select id="monthdropdown">


</select>


<select id="yeardropdown">


</select>


</form>


<script type="text/javascript">


window.onload=function(){


populatedropdown("yeardropdown", "monthdropdown", "daydropdown")


}


</script>

'개발 > JavaScript' 카테고리의 다른 글

배열  (0) 2018.02.13
전역, 지역 변수  (0) 2018.02.13
substring 과 substr의 차이  (0) 2018.02.13
프롬프트 창 띄우기  (0) 2018.02.13
window.opne 에 url 들어갈 때  (0) 2018.02.13

+ Recent posts