怎么利用JS实现两个select列表对年份、月份进行循环以及默认选中当前年份、当前的月份?请高手指教…
1个回答
展开全部
<script type="text/javascript">
window.onload = function()
{
var now = new Date();
var currentYear = now.getFullYear();
var currentMonth = now.getMonth() + 1;
BindSelect(document.getElementById('year'), currentYear - 50, currentYear + 10, currentYear);
BindSelect(document.getElementById('month'), 1, 12, currentMonth);
}
function BindSelect(selectObj, startValue, endValue, selectedValue)
{
var i = 0;
for(var val = startValue; val <= endValue; val++)
{
selectObj.options.add(new Option(val , val));
if(val == selectedValue)selectObj.options[i].selected = true;
i++;
}
}
</script>
<select id="year"></select>年<select id="month"></select>月
window.onload = function()
{
var now = new Date();
var currentYear = now.getFullYear();
var currentMonth = now.getMonth() + 1;
BindSelect(document.getElementById('year'), currentYear - 50, currentYear + 10, currentYear);
BindSelect(document.getElementById('month'), 1, 12, currentMonth);
}
function BindSelect(selectObj, startValue, endValue, selectedValue)
{
var i = 0;
for(var val = startValue; val <= endValue; val++)
{
selectObj.options.add(new Option(val , val));
if(val == selectedValue)selectObj.options[i].selected = true;
i++;
}
}
</script>
<select id="year"></select>年<select id="month"></select>月
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询