gpt4 book ai didi

javascript - 使用 Javascript 的下拉列表

转载 作者:行者123 更新时间:2023-11-30 14:18:29 25 4
gpt4 key购买 nike

如何仅使用 JS 创建下拉列表?示例我有我的 HTML,我知道对于 HTML 中的下拉列表,您使用选择和选项标签,但是如果我不更改任何 HTML 元素并且只使用 Javascript 并链接其中的 html 以使用 JS 创建日-月-年下拉列表,那将如何完成?

我的 HTML:

<!doctype html>
<html lang="en">
<head>
<title> Testing for JS Dropdown</title>
<style>
div {
width: 50%;
margin: auto;
height: 200px;
border: 5px solid black;
background-color: silver;
}
.field {
padding-left: 10px;
width: 60px;
display: inline-block;
}
h2 { padding-left: 10px;}
</style>
<meta charset="utf-8">
<script src="prototype.js"></script>
<script src="JStester.js"></script>
</head>
<body>
<div>
<h2> Select Day-Month-Year </h2>
<p> <span class="field">Day </span><select id="days"> </select></p>
<p> <span class="field">Month</span><select id="months"> </select></p>
<p> <span class="field">Year</span><select id="years"></select></p>
</div>
</body>
</html>

最佳答案

试试这个..

$(document).ready(function() {
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var qntYears = 4;
var selectYear = $("#years");
var selectMonth = $("#months");
var selectDay = $("#days");
var currentYear = new Date().getFullYear();

for (var y = 0; y < qntYears; y++){
let date = new Date(currentYear);
var yearElem = document.createElement("option");
yearElem.value = currentYear
yearElem.textContent = currentYear;
selectYear.append(yearElem);
currentYear--;
}

for (var m = 0; m < 12; m++){
let monthNum = new Date(2018, m).getMonth()
let month = monthNames[monthNum];
var monthElem = document.createElement("option");
monthElem.value = monthNum;
monthElem.textContent = month;
selectMonth.append(monthElem);
}

var d = new Date();
var month = d.getMonth();
var year = d.getFullYear();
var day = d.getDate();

selectYear.val(year);
selectYear.on("change", AdjustDays);
selectMonth.val(month);
selectMonth.on("change", AdjustDays);

AdjustDays();
selectDay.val(day)

function AdjustDays(){
var year = selectYear.val();
var month = parseInt(selectMonth.val()) + 1;
selectDay.empty();

//get the last day, so the number of days in that month
var days = new Date(year, month, 0).getDate();

//lets create the days of that month
for (var d = 1; d <= days; d++){
var dayElem = document.createElement("option");
dayElem.value = d;
dayElem.textContent = d;
selectDay.append(dayElem);
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html lang="en">
<head>
<title> Testing for JS Dropdown</title>
<style>
div {
width: 50%;
margin: auto;
height: 200px;
border: 5px solid black;
background-color: silver;
}
.field {
padding-left: 10px;
width: 60px;
display: inline-block;
}
h2 { padding-left: 10px;}
</style>
<meta charset="utf-8">
<script src="prototype.js"></script>
<script src="JStester.js"></script>
</head>
<body>
<div>
<h2> Select Day-Month-Year </h2>
<p> <span class="field">Day </span><select id="days"> </select></p>
<p> <span class="field">Month</span><select id="months"> </select></p>
<p> <span class="field">Year</span><select id="years"></select></p>
</div>
</body>
</html>

关于javascript - 使用 Javascript 的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53149746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com