2014. 5. 29. 11:43

[자바스크립트][예제] innerHTML로 <select><option> 으로 날짜 구현하기




처음 페이지가 열릴때 (window.onload) 동적으로

날짜를 select박스에 넣는 예제!


Year

[1980]~[2019]년까지..

Day

[1]~[12]월까지,

Month

[1]~[31]일까지 넣기!



<!-- 자바스크립트로 구현! -->

<script language="javascript">

window.onload = function(){

var sYear=1980

var eYear=2019

var sMonth=1

var eMonth=12

var sDay=1

var eDay=31


var strYear="";

var strMonth="";

var strDay="";


for(var i=sYear; i<=eYear; i++)

{

strYear +="<option value="+i+">"+i+"</option>";

}


for(var i=sMonth; i<=eMonth; i++)

{

strMonth +="<option value="+i+">"+i+"</option>";

}


for(var i=sDay; i<=eDay; i++)

{

strDay +="<option value="+i+">"+i+"</option>";

}


document.getElementById("year").innerHTML = strYear

     document.getElementById("month").innerHTML = strMonth

     document.getElementById("day").innerHTML = strDay




}

</script>

<!-- HTML 코드! -->

<body>

<table border=1>

<tr>

<td>

날짜선택

</td>

<td>

<select id="year" name="year">

</select>

</td>

<td>

<select id="month" name="month">

</select>

</td>

<td>

<select id="day" name="day">

</select>

</td>

</tr>

</table>

</body>


결과!





Posted by Triany