时间:2021-07-01 10:21:17 帮助过:10人阅读
<SELECT NAME="SelTjYear">
</SELECT>
<SELECT NAME="SelTjMonth">
</SELECT>
源码:
代码如下:
function vYearMonth(yearObjId, monthObjId) {
var selYear = document.getElementById(yearObjId);
var selMonth = document.getElementById(monthObjId);
var myDate = new Date(); //当前日期
var myYear = myDate.getFullYear(); //当前年
var myMonth = myDate.getMonth() + 1; //当前月
var yearMin = -2; //年份范围值,也当前年比较的差值
var yearMax = 10; //年份范围值,也当前年比较的差值
//Begin年*******************************
selYear.options.add(new Option("", ""));
for (var i = yearMin; i < yearMax; i++) {
var opt = new Option(myYear + i, myYear + i);
selYear.options.add(opt);
}
//这里1-yearMin表示选中当前年,用1是因为开头有插入""
selYear.options.selectedIndex = 1 - yearMin;
//End年*******************************
//Begin月*******************************
selMonth.options.add(new Option("", ""));
for (var i = 0; i < 12; i++) {
var opt = new Option(i + 1, i + 1);
selMonth.options.add(opt);
}
//选中当前月
selMonth.options.selectedIndex = myMonth;
//End月*******************************
selYear.onchange = function () {
if (this.value == "") {
selMonth.selectedIndex = 0;
}
else {
if (selMonth.value == "") {
selMonth.selectedIndex = myMonth;
}
}
};
}
调用:
代码如下:
vYearMonth('SelTjYear', 'SelTjMonth');
完整的演示代码:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]