简单的年月联动
时间:2010-11-15 来源:忧郁的匹格
<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');
相关阅读 更多 +