JS下拉框年月


項目中用到的一些簡單JS小知識,動態創建年月下拉框:

 

 

JS代碼:

 

 

 

var date = new Date();

var y = date.getFullYear();

var m = date.getMonth() + 1;

 

for (i = 0; i < 10; i++) {

var oP = document.createElement("option");

var oText = document.createTextNode(y);

oP.appendChild(oText);

oP.setAttribute("value", y);

document.getElementById('year').appendChild(oP);

y = y - 1;

};

 

var j = 1;

 

for (i = 1; i < 13; i++) {

var month = document.createElement("option");

var monthText = document.createTextNode(j);

month.appendChild(monthText);

month.setAttribute("value", j);

if (j == m) {

month.setAttribute("selected", "selected");

}

;

document.getElementById('month').appendChild(month);

j = j + 1;

};

 

 

JSP頁面:

<table >

<tr>

<td>

年份:

<select id="year">

 

</select>

 

</td>

<td>

月份:

<select id="month">

 

</select>

 

</td>

<td>

<input type="button" value="確定" onclick="show()" />

</td>

</tr>

 

</table>

 

效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM