如何仅使用JS创建下拉列表? 示例我有我的HTML,我知道对于HTML的下拉列表,您可以使用select和option标记,但是如果我不更改任何HTML元素,而仅使用Javascript并链接其中的html,则可以使用JS创建Day-Month-Year一年的下拉列表,该怎么做?
我的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>
答案 0 :(得分:1)
尝试一下。
$(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>
答案 1 :(得分:1)
仅使用JAVASCRIPT尝试
UPDATE TransmitterPointsData
SET DynamicDistance = GETDISTANCE(Lat, Longi, Latitude, Longitude)