Home > Web Front-end > JS Tutorial > js simple year and month linkage implementation code_time and date

js simple year and month linkage implementation code_time and date

WBOY
Release: 2016-05-16 18:16:16
Original
1168 people have browsed it

HTML

Copy code The code is as follows:




Source code:
Copy 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; //Year range value, also the difference compared with the current year
var yearMax = 10; //Year range value, also compared with the current year Difference

//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 means the current year is selected, 1 is used because "" is inserted at the beginning
selYear.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 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 code The code is as follows:

vYearMonth('SelTjYear', ' SelTjMonth');

Full demo code:

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> function vYearMonth(yearObjId, monthObjId) { var selYear = document.getElementById(yearObjId); var selMonth = document.getElementById(monthObjId); var myDate = new Date(); //当前日期 var myYear = myDate.getFullYear(); //当前年 var myMonth = myDate.getMonth() + 1; //当前月 var yearMin = -2; //年份范围值,也当前年比较的差值 var yearMax = 10; //年份范围值,也当前年比较的差值 //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); } //这里1-yearMin表示选中当前年,用1是因为开头有插入"" selYear.options.selectedIndex = 1 - yearMin; //End年******************************* //Begin月******************************* selMonth.options.add(new Option("", "")); for (var i = 0; i < 12; i++) { var opt = new Option(i + 1, i + 1); selMonth.options.add(opt); } //选中当前月 selMonth.options.selectedIndex = myMonth; //End月******************************* selYear.onchange = function () { if (this.value == "") { selMonth.selectedIndex = 0; } else { if (selMonth.value == "") { selMonth.selectedIndex = myMonth; } } }; } vYearMonth('SelTjYear', 'SelTjMonth'); </script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template