JavaScript code is implemented by simple year-on-year interaction. For more information, see. HTML
The Code is as follows:
Source code:
The Code is as follows:
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; // The year range value, which is also the difference value of the current year comparison.
Var yearMax = 10; // The year range value, which is also the difference value of the current year comparison.
// * In *******************************
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 indicates that the current year is selected, and 1 is used because "" is inserted at the beginning ""
SelYear. options. selectedIndex = 1-yearMin;
// End year *******************************
// In 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;
}
}
};
}
Call:
The Code is as follows:
VYearMonth ('seltjyear', 'seltjmonth ');
Complete DEMO code:
<Select name = "SelTjYear"> </SELECT> <select name = "SelTjMonth"> </SELECT> script function vYearMonth (yearObjId, monthObjId) {var selYear = document. getElementById (yearObjId); var selMonth = document. getElementById (monthObjId); var myDate = new Date (); // the current Date var myYear = myDate. getFullYear (); // var myMonth = myDate of the current year. getMonth () + 1; // var yearMin =-2 for the current month; // The year range value, which is also the difference var yearMax = 10 for the current year comparison; // The year range value, the difference value of the current year comparison:/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 indicates the current year, and 1 is used because "" selYear "is inserted at the beginning. 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 selMonth of the current month. options. selectedIndex = myMonth; // End month ******************************** selYear. onchange = function () {if (this. value = "") {selMonth. selectedIndex = 0;} else {if (selMonth. value = "") {selMonth. selectedIndex = myMonth ;}};} vYearMonth ('seltjyear', 'seltjmonth'); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]