JS Simple month-by-Day Linkage Implementation code _ time and date

Source: Internet
Author: User
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]

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.