Html
Copy Code code as follows:
<select name= "Seltjyear" >
</SELECT>
<select name= "Seltjmonth" >
</SELECT>
Source:
Copy Code code 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 (); When the year before last
var mymonth = mydate.getmonth () + 1; Current month
var yearmin =-2; Year range value, also when the difference is compared
var Yearmax = 10; Year range value, also when the difference is compared
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 that the current year is selected, with 1 because the insertion "" is at the beginning.
SelYear.options.selectedIndex = 1-yearmin;
End year *******************************
Begin Month *******************************
SelMonth.options.add (New Option ("", ""));
for (var i = 0; i < i++) {
var opt = new Option (i + 1, i + 1);
SelMonth.options.add (opt);
}
Select 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 Code code as follows:
Vyearmonth (' seltjyear ', ' seltjmonth ');
Complete Demo Code:
<select name= "Seltjyear" > </SELECT> <select name= "Seltjmonth" > </SELECT> <script> Fun Ction 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 when the difference between the previous years var Yearmax = 10; Year range value, also when the difference between the previous year's//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); }//Here 1-yearmin indicates that the current year is selected with 1 because the opening has the insertion "" SelYear.options.selectedIndex = 1-yearmin; End year *******************************//begin month ******************************* selMonth.options.add (new Option (""), "")); for (var i = 0; i < i++) {var opt = new Option (i + 1, i + 1); SelMonth.options.add (opt); //select Current Month Selmonth.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 All SELECT Note: If the need to introduce external JS need to refresh to perform]