用JavaScript写一个日期下拉菜单
时间: 2023-04-02 16:03:02 浏览: 110
好的,我可以回答这个问题。以下是用 JavaScript 写一个日期下拉菜单的代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Date Dropdown Menu</title>
</head>
<body>
<label for="date">选择日期:</label>
<select id="date">
<option value="">请选择</option>
</select>
<script>
var dateSelect = document.getElementById("date");
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth() + 1;
var currentDay = currentDate.getDate();
for (var i = ; i < 365; i++) {
var date = new Date(currentYear, , 1);
date.setDate(date.getDate() + i);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var option = document.createElement("option");
option.value = year + "-" + month + "-" + day;
option.text = year + "年" + month + "月" + day + "日";
if (year == currentYear && month == currentMonth && day == currentDay) {
option.selected = true;
}
dateSelect.appendChild(option);
}
</script>
</body>
</html>
```
这个代码会生成一个日期下拉菜单,包含从当前日期往后的一年内的所有日期选项。用户可以从中选择一个日期,然后提交表单或执行其他操作。
阅读全文