//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');
完整的演示代码: