如何制作自定义的月份和年份选择器?

huangapple go评论56阅读模式
英文:

how to make custom month and year picker?

问题

我正在尝试创建一个自定义的月份和年份选择器。我知道有一些插件可以实现这个功能,但我想要一个简单的月份和年份选择器。在这段代码中,如果用户选择的月份小于当前月份,我已经禁用了2023年的选项。代码似乎运行完美,但存在一个小问题。

  1. 当页面加载时,应该显示2023年,但它显示为2024年。
  2. 当用户选择当前月份(即7月)时,禁用了当前年份(即2023年),这不应该发生。但这只在页面加载时发生,如果用户从其他月份切换到7月,则可以选择2023年。

考虑以下情况:用户打开页面,显示的是2024年7月而不是2023年7月。现在用户点击年份,2023年被禁用了。然后用户点击月份并选择11月。然后再次选择7月,现在2023年可以选择。理解这种情况意味着问题只出现在页面加载时。

let selectedYear = new Date().getFullYear();

function updateYearOptions() {
  const currentMonth = new Date().getMonth() + 1;
  const selectedMonth = parseInt(document.getElementById('month').value);

  const yearSelect = document.getElementById('year');
  const currentYear = new Date().getFullYear();
  const maxYear = currentYear + 20;

  yearSelect.innerHTML = '';

  let yearToSelect = selectedYear;

  for (let year = currentYear; year <= maxYear; year++) {
    const option = document.createElement('option');
    option.value = year;
    option.textContent = year;

    if (year === currentYear && selectedMonth < currentMonth) {
      option.disabled = true;
    }

    if (year === selectedYear && option.disabled) {
      yearToSelect = year + 1;
    }

    yearSelect.appendChild(option);
  }

  yearSelect.value = yearToSelect;
  selectedYear = yearToSelect;
  document.getElementById('selectedYear').value = yearToSelect;
}

function updateMonthOptions() {
  const monthSelect = document.getElementById('month');
  const currentMonth = new Date().getMonth() + 1;
  monthSelect.value = currentMonth;
}

updateYearOptions();
updateMonthOptions();

function updateSelectedYear() {
  selectedYear = parseInt(document.getElementById('year').value);
  document.getElementById('selectedYear').value = selectedYear;
}
body {
  font-family: Arial, sans-serif;
  text-align: center;
  margin-top: 50px;
}

#datepicker {
  width: 400px;
  padding: 10px;
  border: 1px solid #ccc;
}
<div id="datepicker">
  <label for="month">Month:</label>
  <select id="month" onchange="updateYearOptions()">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
  </select>

  <label for="year">Year:</label>
  <select id="year" onchange="updateSelectedYear()"></select>
  <input type="hidden" id="selectedYear" name="selectedYear">
</div>
英文:

I am trying to make a custom month and year picker. I know there are plugins for it. but I wanted a simple month and year picker. In this code, I have disabled the the year 2023 if the month chosen by user is less than the current month. The code seems to work perfect but there is a minor issue.

  1. The year when the page loads should be shown as 2023 but it is displaying 2024.
  2. The current year which is 2023 gets disabled when the user selects current month which is July. this shouldn't happen. but this happens only in the start when the page is loaded. but if the user switches from another month to July again then the user is able to select 2023.
    Take this scenario: user opens the page. there is July 2024 instead of July 2023. now the user clicks on year, the 2023 is disabled. now the user clicks on month and selects November. then again after choosing November he choses July again, now the year 2023 is selectable. understanding this scenario means that the issue is only in the start when the page loads.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let selectedYear = new Date().getFullYear();
function updateYearOptions() {
const currentMonth = new Date().getMonth() + 1;
const selectedMonth = parseInt(document.getElementById(&#39;month&#39;).value);
const yearSelect = document.getElementById(&#39;year&#39;);
const currentYear = new Date().getFullYear();
const maxYear = currentYear + 20;
yearSelect.innerHTML = &#39;&#39;;
let yearToSelect = selectedYear; // Store the year to select (might change below)
for (let year = currentYear; year &lt;= maxYear; year++) {
const option = document.createElement(&#39;option&#39;);
option.value = year;
option.textContent = year;
if (year === currentYear &amp;&amp; selectedMonth &lt; currentMonth) {
option.disabled = true;
}
if (year === selectedYear &amp;&amp; option.disabled) {
yearToSelect = year + 1; // If selected year is disabled, set the yearToSelect to the next available year
}
yearSelect.appendChild(option);
}
yearSelect.value = yearToSelect;
selectedYear = yearToSelect;
document.getElementById(&#39;selectedYear&#39;).value = yearToSelect;
}
function updateMonthOptions() {
const monthSelect = document.getElementById(&#39;month&#39;);
const currentMonth = new Date().getMonth() + 1;
monthSelect.value = currentMonth;
}
updateYearOptions();
updateMonthOptions();
function updateSelectedYear() {
selectedYear = parseInt(document.getElementById(&#39;year&#39;).value);
document.getElementById(&#39;selectedYear&#39;).value = selectedYear;
}

<!-- language: lang-css -->

body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
#datepicker {
width: 400px;
padding: 10px;
border: 1px solid #ccc;
}

<!-- language: lang-html -->

&lt;div id=&quot;datepicker&quot;&gt;
&lt;label for=&quot;month&quot;&gt;Month:&lt;/label&gt;
&lt;select id=&quot;month&quot; onchange=&quot;updateYearOptions()&quot;&gt;
&lt;option value=&quot;1&quot;&gt;January&lt;/option&gt;
&lt;option value=&quot;2&quot;&gt;February&lt;/option&gt;
&lt;option value=&quot;3&quot;&gt;March&lt;/option&gt;
&lt;option value=&quot;4&quot;&gt;April&lt;/option&gt;
&lt;option value=&quot;5&quot;&gt;May&lt;/option&gt;
&lt;option value=&quot;6&quot;&gt;June&lt;/option&gt;
&lt;option value=&quot;7&quot;&gt;July&lt;/option&gt;
&lt;option value=&quot;8&quot;&gt;August&lt;/option&gt;
&lt;option value=&quot;9&quot;&gt;September&lt;/option&gt;
&lt;option value=&quot;10&quot;&gt;October&lt;/option&gt;
&lt;option value=&quot;11&quot;&gt;November&lt;/option&gt;
&lt;option value=&quot;12&quot;&gt;December&lt;/option&gt;
&lt;/select&gt;
&lt;label for=&quot;year&quot;&gt;Year:&lt;/label&gt;
&lt;select id=&quot;year&quot; onchange=&quot;updateSelectedYear()&quot;&gt;
&lt;/select&gt;
&lt;input type=&quot;hidden&quot; id=&quot;selectedYear&quot; name=&quot;selectedYear&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

updateYearOptions首次运行时,您的月份选择器的值为1,小于7,满足禁用2023年选项的条件。

只需交换初始化时运行的函数的顺序,使您的月份首先设置,这样在进行年份计算比较时就是正确的。

updateMonthOptions();
updateYearOptions();

请注意,这是代码部分的翻译。

英文:

When updateYearOptions runs for the first time, the value of your month selector is 1 which is less than 7, which fulfills the criteria for disabiling the option for 2023.

Just need to swap the order of the functions that run on init so your month is set first, that way it is correct when compared for the year calculation.

updateMonthOptions();
updateYearOptions();

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let selectedYear = new Date().getFullYear();
function updateYearOptions() {
const currentMonth = new Date().getMonth() + 1;
console.info(&#39;Current Month&#39;, currentMonth);
const selectedMonth = parseInt(document.getElementById(&#39;month&#39;).value);
console.info(&#39;Selected Month&#39;, selectedMonth);
const yearSelect = document.getElementById(&#39;year&#39;);
const currentYear = new Date().getFullYear();
const maxYear = currentYear + 20;
yearSelect.innerHTML = &#39;&#39;;
let yearToSelect = selectedYear; // Store the year to select (might change below)
for (let year = currentYear; year &lt;= maxYear; year++) {
const option = document.createElement(&#39;option&#39;);
option.value = year;
option.textContent = year;
if (year === currentYear &amp;&amp; selectedMonth &lt; currentMonth) {
option.disabled = true;
}
if (year === selectedYear &amp;&amp; option.disabled) {
yearToSelect = year + 1; // If selected year is disabled, set the yearToSelect to the next available year
}
yearSelect.appendChild(option);
}
yearSelect.value = yearToSelect;
selectedYear = yearToSelect;
document.getElementById(&#39;selectedYear&#39;).value = yearToSelect;
}
function updateMonthOptions() {
const monthSelect = document.getElementById(&#39;month&#39;);
const currentMonth = new Date().getMonth() + 1;
monthSelect.value = currentMonth;
}
updateMonthOptions();
updateYearOptions();
function updateSelectedYear() {
selectedYear = parseInt(document.getElementById(&#39;year&#39;).value);
document.getElementById(&#39;selectedYear&#39;).value = selectedYear;
}

<!-- language: lang-css -->

body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
#datepicker {
width: 400px;
padding: 10px;
border: 1px solid #ccc;
}

<!-- language: lang-html -->

&lt;div id=&quot;datepicker&quot;&gt;
&lt;label for=&quot;month&quot;&gt;Month:&lt;/label&gt;
&lt;select id=&quot;month&quot; onchange=&quot;updateYearOptions()&quot;&gt;
&lt;option value=&quot;1&quot;&gt;January&lt;/option&gt;
&lt;option value=&quot;2&quot;&gt;February&lt;/option&gt;
&lt;option value=&quot;3&quot;&gt;March&lt;/option&gt;
&lt;option value=&quot;4&quot;&gt;April&lt;/option&gt;
&lt;option value=&quot;5&quot;&gt;May&lt;/option&gt;
&lt;option value=&quot;6&quot;&gt;June&lt;/option&gt;
&lt;option value=&quot;7&quot;&gt;July&lt;/option&gt;
&lt;option value=&quot;8&quot;&gt;August&lt;/option&gt;
&lt;option value=&quot;9&quot;&gt;September&lt;/option&gt;
&lt;option value=&quot;10&quot;&gt;October&lt;/option&gt;
&lt;option value=&quot;11&quot;&gt;November&lt;/option&gt;
&lt;option value=&quot;12&quot;&gt;December&lt;/option&gt;
&lt;/select&gt;
&lt;label for=&quot;year&quot;&gt;Year:&lt;/label&gt;
&lt;select id=&quot;year&quot; onchange=&quot;updateSelectedYear()&quot;&gt;
&lt;/select&gt;
&lt;input type=&quot;hidden&quot; id=&quot;selectedYear&quot; name=&quot;selectedYear&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月20日 22:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定