function vYearMonth(yearObjId, monthObjId) { var selYear = document.getElementById(yearObjId); var selMonth = document.getElementById(monthObjId); var myDate = new Date(); //Current date var myYear = myDate.getFullYear(); //Current year var myMonth = myDate. getMonth() 1; //Current month var yearMin = -2; //Year range value, also the difference compared with the current year var yearMax = 10; //Year range value, also compared with the current year Difference
//Begin year********************************** selYear.options. add(new Option("", "")); for (var i = yearMin; i < yearMax; i ) { var opt = new Option(myYear i, myYear i); selYear.options.add(opt); } //Here 1-yearMin means the current year is selected, 1 is used because "" is inserted at the beginning selYear.options.selectedIndex = 1 - yearMin; //End year****************************
//Begin month*** **************************** selMonth.options.add(new Option("", "")); for (var i = 0; i < 12; i ) { var opt = new Option(i 1, i 1); selMonth.options.add(opt); } //Select the current month selMonth.options.selectedIndex = myMonth; //End month************************** *****
selYear.onchange = function () { if (this.value == "") { selMonth.selectedIndex = 0; } else { if (selMonth.value == "") { selMonth.selectedIndex = myMonth; } } }; }
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn