[英]Display DIV for current month from months dropdown options
I have a drop down of months (January to December).我有几个月的时间(一月到十二月)。 When selecting a month, it would display a DIV for that month.选择月份时,它将显示该月份的 DIV。 By default , the current month and the corresponding DIV would be displayed .默认情况下,将显示当前月份和相应的 DIV 。
So far, I am able to display the current month in the drop down by default, but I don't know why the corresponding DIV is not displayed.到目前为止,我可以默认在下拉菜单中显示当前月份,但我不知道为什么没有显示相应的DIV。
Furthermore, when January is selected, the DIV displays, but then does not go away when another month is selected. Furthermore, when January is selected, the DIV displays, but then does not go away when another month is selected. This is only the case for the month of January.这只适用于 1 月份。
HTML HTML
<select name="month" id="month">
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
<option value="3">April</option>
<option value="4">May</option>
<option value="5">June</option>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>
</select>
<div class="hide" id="0">Jan content</div>
<div class="hide" id="1">Feb content</div>
<div class="hide" id="2">Mar content</div>
<div class="hide" id="3">Apr content</div>
<div class="hide" id="4">May content</div>
<div class="hide" id="5">Jun content</div>
<div class="hide" id="6">Jul content</div>
<div class="hide" id="7">Aug content</div>
<div class="hide" id="8">Sept content</div>
<div class="hide" id="9">Oct content</div>
<div class="hide" id="10">Nov content</div>
<div class="hide" id="11">Dec content</div>
CSS CSS
.hide {
display: none;
}
JavaScript + jQuery JavaScript + jQuery
var CurrentDate=new Date();
$("#month").val(CurrentDate.getMonth());
document.getElementById('month').onchange = function() {
var i = 1;
var myDiv = document.getElementById(i);
while(myDiv) {
myDiv.style.display = 'none';
myDiv = document.getElementById(++i);
}
document.getElementById(this.value).style.display = 'block';
};
Demo: https://jsfiddle.net/mp61hbno/演示: https://jsfiddle.net/mp61hbno/
Thank you!谢谢!
I think you were really close.我想你真的很亲近。 Have a look at the code below.看看下面的代码。 I modified a few things.我修改了一些东西。
Specifically var i = 0;
特别var i = 0;
and also the initial display.以及初始显示。
var CurrentDate=new Date(); $("#month").val(CurrentDate.getMonth()); document.getElementById(new Date().getMonth()).style.display = 'block'; document.getElementById('month').onchange = function() { var i = 0; var myDiv = document.getElementById(i); while(myDiv) { myDiv.style.display = 'none'; myDiv = document.getElementById(++i); } document.getElementById(this.value).style.display = 'block'; };
.hide { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="month" id="month"> <option value="0">January</option> <option value="1">February</option> <option value="2">March</option> <option value="3">April</option> <option value="4">May</option> <option value="5">June</option> <option value="6">July</option> <option value="7">August</option> <option value="8">September</option> <option value="9">October</option> <option value="10">November</option> <option value="11">December</option> </select> <div class="hide" id="0">Jan content</div> <div class="hide" id="1">Feb content</div> <div class="hide" id="2">Mar content</div> <div class="hide" id="3">Apr content</div> <div class="hide" id="4">May content</div> <div class="hide" id="5">Jun content</div> <div class="hide" id="6">Jul content</div> <div class="hide" id="7">Aug content</div> <div class="hide" id="8">Sept content</div> <div class="hide" id="9">Oct content</div> <div class="hide" id="10">Nov content</div> <div class="hide" id="11">Dec content</div>
You were not showing the content of current month, see this which might help you.你没有显示当月的内容,看看这个可能对你有帮助。
var CurrentDate=new Date(); var curMonth = CurrentDate.getMonth(); document.getElementById(curMonth).style.display = 'block'; $("#month").val(CurrentDate.getMonth()); document.getElementById('month').onchange = function() { var i = 1; var myDiv = document.getElementById(i); while(myDiv) { myDiv.style.display = 'none'; myDiv = document.getElementById(++i); } document.getElementById(this.value).style.display = 'block'; };
.hide { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="month" id="month"> <option value="0">January</option> <option value="1">February</option> <option value="2">March</option> <option value="3">April</option> <option value="4">May</option> <option value="5">June</option> <option value="6">July</option> <option value="7">August</option> <option value="8">September</option> <option value="9">October</option> <option value="10">November</option> <option value="11">December</option> </select> <div class="hide" id="0">Jan content</div> <div class="hide" id="1">Feb content</div> <div class="hide" id="2">Mar content</div> <div class="hide" id="3">Apr content</div> <div class="hide" id="4">May content</div> <div class="hide" id="5">Jun content</div> <div class="hide" id="6">Jul content</div> <div class="hide" id="7">Aug content</div> <div class="hide" id="8">Sept content</div> <div class="hide" id="9">Oct content</div> <div class="hide" id="10">Nov content</div> <div class="hide" id="11">Dec content</div>
I have made few changes to you js code, try this我对你的 js 代码做了一些改动,试试这个
var CurrentDate=new Date();
var curMnth = CurrentDate.getMonth();
$("#month").val(curMnth);
$("#"+curMnth).show();
document.getElementById('month').onchange = function() {
var i = 0;
var myDiv = document.getElementById(i);
while(myDiv) {
myDiv.style.display = 'none';
myDiv = document.getElementById(++i);
}
document.getElementById(this.value).style.display = 'block';
};
It will solve the issue.它将解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.