栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JavaScript显示最近3个月的下拉列表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JavaScript显示最近3个月的下拉列表

此函数返回

n
最近几个月,包括当前月份:

function getLastMonths(n) {    var months = new Array();    var today = new Date();    var year = today.getFullYear();    var month = today.getMonth() + 1;    var i = 0;    do {        months.push(year + (month > 9 ? "" : "0") + month);        if(month == 1) { month = 12; year--;        } else { month--;        }        i++;    } while(i < n);    return months;}

呼叫:

document.write(getLastMonths(4));

印刷品:

201211,201210,201209,201208

然后,在下拉框中添加这些值非常简单:

function writeMonthOptions() {   var optionValues = getLastMonths(4);   var dropDown = document.getElementById("monthList");   for(var i=0; i<optionValues.length; i++) {       var key = optionValues[i].slice(4,6);       var value = optionValues[i];       dropDown.options[i] = new Option(value, key);    }}

只需使用:

writeMonthOptions();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418898.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号