简体   繁体   English

从月份下拉选项中显示当前月份的 DIV

[英]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.

相关问题 连续显示所有 12 个月,但从当前月份开始 - Display all 12 months serially but starting from the current month 如何在日期选择器中仅显示 3 个月作为当前日期的下拉列表,当当月完成时,我们将显示即将到来的 3 个月 - How to show only 3 months in datepicker as dropdown from current date and when the current month is completed then we have show the upcoming 3 months 仅在月份下拉列表中需要当前月份和未来月份 - Need current month and upcoming months only in month dropdown Cognos-值提示,根据年份显示从一月到当前月份的月份 - Cognos - value prompt to display months from january to current month based on year 使用本月起的月份在javascript中创建下拉列表 - create a dropdown list in javascript using months from this month 如何在3个月内显示来自数据库的div内容 - How to Display div in 3 months content from database 从当前月份开始的月份中打印3年日历 - Printing 3 year calendar in months staring from the current month 隐藏前几个月从当月开始 - hide all previous months start from the current month 如何使用 JavaScript 从月份列表中选择当前月份? - How to select current month from a list of months with JavaScript? 在 javascript 中构建从当前月份开始的前三个月的数组 - Build an array with previous three months from current month in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM