求各位帮个忙,想实现下拉菜单月份从当月到12月份,请问我这个有什么问题?
window.onload = function () {
// 获取当前月份(注意:getMonth() 返回值是 0 - 11,所以要加 1)
var currentMonth = new Date().getMonth() + 1;
// 获取下拉框元素,这里需要根据实际的下拉框 ID 进行修改
var selectElement = document.getElementById('14426');
// 循环从当前月份到 12 月份
for (var i = currentMonth; i <= 12; i++) {
// 创建一个新的 option 元素
var option = document.createElement('option');
// 设置 option 的值为当前月份
option.value = i;
// 设置 option 的显示文本为当前月份
option.text = i + '月';
// 将 option 添加到下拉框中
selectElement.add(option);
}
};
window.onload = function () {
// 获取当前月份(注意:getMonth() 返回值是 0 - 11,所以要加 1)
var currentMonth = new Date().getMonth() + 1;
// 获取下拉框元素,这里需要根据实际的下拉框 ID 进行修改
var selectElement = document.getElementById('14426');
// 循环从当前月份到 12 月份
for (var i = currentMonth; i <= 12; i++) {
// 创建一个新的 option 元素
var option = document.createElement('option');
// 设置 option 的值为当前月份
option.value = i;
// 设置 option 的显示文本为当前月份
option.text = i + '月';
// 将 option 添加到下拉框中
selectElement.add(option);
}
};