Js simple implementation code of year-on-year Interaction

Source: Internet
Author: User

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.