javascript - 使用纯 JavaScript 使用当前日期、月份和年份填充下拉列表
问题描述
我发现了一个我感兴趣的问题,并在此链接上找到了解决方案: https ://stackoverflow.com/a/51660131/17222337 ,但它在 jQuery 中,但我对纯 JavaScript 解决方案感兴趣。我回顾了我能做什么,但在纯 javascript 中只发现了关于这个主题的非工作代码。我试图从 jQuery 翻译成 javascript,但我得到的代码是错误的并且不起作用。请告诉我,如何从给定的 jquery 代码制作 javascript 代码?
解决方案
我找到了解决方案。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
}
body{
min-height: 100vh;
display: flex;
justify-content: center;
align-items:center;
flex-direction: column;
}
form span{
display: block;
margin: 20px 0;
}
/*make form styling consistent across browsers*/
button, input, select, textarea {
font-family: inherit;
font-size: 100%;
}
</style>
</head>
<body>
<form>
<span>
<label for="day">Day:</label>
<select name="day" id="day"></select>
</span>
<span>
<label for="month">Month:</label>
<select name="month" id="month"></select>
</span>
<span>
<label for="year">Year:</label>
<select name="year" id="year">Year:</select>
</span>
</form>
<script>
//Create references to the dropdown's
const yearSelect = document.getElementById("year");
const monthSelect = document.getElementById("month");
const daySelect = document.getElementById("day");
const months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'];
//Months are always the same
(function populateMonths(){
for(let i = 0; i < months.length; i++){
const option = document.createElement('option');
option.textContent = months[i];
monthSelect.appendChild(option);
}
monthSelect.value = "January";
})();
let previousDay;
function populateDays(month){
//Delete all of the children of the day dropdown
//if they do exist
while(daySelect.firstChild){
daySelect.removeChild(daySelect.firstChild);
}
//Holds the number of days in the month
let dayNum;
//Get the current year
let year = yearSelect.value;
if(month === 'January' || month === 'March' ||
month === 'May' || month === 'July' || month === 'August'
|| month === 'October' || month === 'December') {
dayNum = 31;
} else if(month === 'April' || month === 'June'
|| month === 'September' || month === 'November') {
dayNum = 30;
}else{
//Check for a leap year
if(new Date(year, 1, 29).getMonth() === 1){
dayNum = 29;
}else{
dayNum = 28;
}
}
//Insert the correct days into the day <select>
for(let i = 1; i <= dayNum; i++){
const option = document.createElement("option");
option.textContent = i;
daySelect.appendChild(option);
}
if(previousDay){
daySelect.value = previousDay;
if(daySelect.value === ""){
daySelect.value = previousDay - 1;
}
if(daySelect.value === ""){
daySelect.value = previousDay - 2;
}
if(daySelect.value === ""){
daySelect.value = previousDay - 3;
}
}
}
function populateYears(){
//Get the current year as a number
let year = new Date().getFullYear();
//Make the previous 100 years be an option
for(let i = 0; i < 101; i++){
const option = document.createElement("option");
option.textContent = year - i;
yearSelect.appendChild(option);
}
}
populateDays(monthSelect.value);
populateYears();
yearSelect.onchange = function() {
populateDays(monthSelect.value);
}
monthSelect.onchange = function() {
populateDays(monthSelect.value);
}
daySelect.onchange = function() {
previousDay = daySelect.value;
}
</script>
</body>
</html>
推荐阅读
- python - 我生成的 python-docx lambda 层有问题吗?或者我的 docx 生成器代码中有什么东西?
- java - Freemarker - 将一个对象打印到另一个对象中
- mysql - 安装后没有 MySQL 应用程序
- sql - 为什么我的查询在 Visual Studio 2010 报表生成器中不起作用
- python - 如何在调用函数时强制命名函数的必需参数,而不给参数提供默认值?
- python - 以某个字符结尾的正则表达式应避免在此之前多次出现该字符
- reactjs - 有没有办法将加载器组件添加到 Material ui Autocomplete 组件
- php - 使用 PHP 将 CSV 文件解析为列
- multiprocessing - fortran 中系统范围的唯一文件名
- java - 我的程序返回多个结果集我可以将它们传递给执行器服务吗?