我正在尝试使用下拉列表中的 javascript 显示当月的最后 3 个月(包括总共四个月)
function writeMonthOptions() {
var months = new Array("01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12");
var today = new Date();
var date = new Date(today);
date.setFullYear(date.getFullYear() - 1);
var dropDown = document.getElementById("Month_list");
var i = today.getMonth();
var optionNames;
var optionValues;
var beanData = '<s:property value="month" />';
while (i < (today.getMonth() + 4)) {
optionNames = months[i];
optionValues = today.getFullYear() + '' + months[i];
dropDown.options[i++] = new Option(optionNames, optionValues);
if (beanData) {
if (beanData == (today.getFullYear() + "" + monthsInd[today
.getMonth()])) {
dropDown.selectedIndex = i - 1;
}
}
}
}
简而言之,在视图部分显示当前月份和前 3 个月的下拉列表。
到后端需要以 [YYYYMM] 形式提交。
如果 bean 中已经存在值,则将其显示为在下拉列表中选择。 [保证会在那4个月内]
该函数返回n
过去几个月,包括当前月份:
function getLastMonths(n) {
var months = new Array();
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var i = 0;
do {
months.push(year + (month > 9 ? "" : "0") + month);
if(month == 1) {
month = 12;
year--;
} else {
month--;
}
i++;
} while(i < n);
return months;
}
Call:
document.write(getLastMonths(4));
Prints:
201211,201210,201209,201208
Demo http://jsfiddle.net/C2Qfe/
然后,在下拉框中添加这些值非常简单:
function writeMonthOptions() {
var optionValues = getLastMonths(4);
var dropDown = document.getElementById("monthList");
for(var i=0; i<optionValues.length; i++) {
var key = optionValues[i].slice(4,6);
var value = optionValues[i];
dropDown.options[i] = new Option(value, key);
}
}
只需使用:
writeMonthOptions();
完整示例 http://jsfiddle.net/C2Qfe/2/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)