英文:
how to make custom month and year picker?
问题
我正在尝试创建一个自定义的月份和年份选择器。我知道有一些插件可以实现这个功能,但我想要一个简单的月份和年份选择器。在这段代码中,如果用户选择的月份小于当前月份,我已经禁用了2023年的选项。代码似乎运行完美,但存在一个小问题。
- 当页面加载时,应该显示2023年,但它显示为2024年。
- 当用户选择当前月份(即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.
- The year when the page loads should be shown as 2023 but it is displaying 2024.
- 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('month').value);
const yearSelect = document.getElementById('year');
const currentYear = new Date().getFullYear();
const maxYear = currentYear + 20;
yearSelect.innerHTML = '';
let yearToSelect = selectedYear; // Store the year to select (might change below)
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; // If selected year is disabled, set the yearToSelect to the next available year
}
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;
}
<!-- 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 -->
<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>
<!-- 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('Current Month', currentMonth);
const selectedMonth = parseInt(document.getElementById('month').value);
console.info('Selected Month', selectedMonth);
const yearSelect = document.getElementById('year');
const currentYear = new Date().getFullYear();
const maxYear = currentYear + 20;
yearSelect.innerHTML = '';
let yearToSelect = selectedYear; // Store the year to select (might change below)
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; // If selected year is disabled, set the yearToSelect to the next available year
}
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;
}
updateMonthOptions();
updateYearOptions();
function updateSelectedYear() {
selectedYear = parseInt(document.getElementById('year').value);
document.getElementById('selectedYear').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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论