HTML
Copy codeThe Code is as follows:
<Select name = "SelTjYear">
</SELECT>
<Select name = "SelTjMonth">
</SELECT>
Source code:
Copy codeThe 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:
Copy codeThe Code is as follows:
VYearMonth ('seltjyear', 'seltjmonth ');
Complete DEMO code:
<Select name = "SelTjYear"> </SELECT> <select name = "SelTjMonth"> </SELECT>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]