JavaScript怎样生成选择可以年月日的联动下拉列表
怎样生成一个选择年月日的下拉表单悬赏分:5-离问题结束还有14天23小时如题:有三个下拉列表,分别为年,月,日。年和月的好实现,循环即可。但是天数却要根据年和月来定。循环...
怎样生成一个选择年月日的下拉表单
悬赏分:5 - 离问题结束还有 14 天 23 小时
如题:
有三个下拉列表,分别为年,月,日。年和月的好实现,循环即可。但是天数却要根据年和月来定。循环次数要看条件。
怎样实现这样的代码。
期待解决…… 展开
悬赏分:5 - 离问题结束还有 14 天 23 小时
如题:
有三个下拉列表,分别为年,月,日。年和月的好实现,循环即可。但是天数却要根据年和月来定。循环次数要看条件。
怎样实现这样的代码。
期待解决…… 展开
3个回答
展开全部
<html>
<head>
<style>
#sYear{width:65px;}
#sMonth{width:50px;}
#sDate{width:50px;}
</style>
<script>
function fillSelect()
{
var ddlYear = document.getElementById("sYear");
var ddlMonth = document.getElementById("sMonth");
var ddlDate = document.getElementById("sDate");
var opt = null;
var curDay = new Date();
var year = curDay.getFullYear();
for (var i = -5; i < 5; i++)
{
opt = document.createElement("OPTION");
opt.value = year + i;
opt.innerText = (year + i) + "年";
ddlYear.appendChild(opt);
}
ddlYear.value = curDay.getFullYear();
for (var i = 1; i <= 12; i++)
{
opt = document.createElement("OPTION");
opt.value = i;
opt.innerText = i + "月";
ddlMonth.appendChild(opt);
}
ddlMonth.value = curDay.getMonth() + 1;
fillDate();
ddlDate.value = curDay.getDate();
}
function fillDate()
{
var ddlYear = document.getElementById("sYear");
var ddlMonth = document.getElementById("sMonth");
var ddlDate = document.getElementById("sDate");
while (ddlDate.options.length > 0)
{
ddlDate.remove(0);
}
var selDay = new Date();
selDay.setFullYear(ddlYear.value, ddlMonth.value, 1);
var lastDay = new Date(selDay - (24 * 60 * 60 * 1000));
var days = lastDay.getDate();
for (var i = 1; i <= days; i++)
{
var opt = document.createElement("OPTION");
opt.value = i;
opt.innerText = i + "日";
ddlDate.appendChild(opt);
}
}
</script>
</head>
<body onload="fillSelect()">
<select id="sYear" onchange="fillDate()"></select>
<select id="sMonth" onchange="fillDate()"></select>
<select id="sDate"></select>
</body>
</html>
<head>
<style>
#sYear{width:65px;}
#sMonth{width:50px;}
#sDate{width:50px;}
</style>
<script>
function fillSelect()
{
var ddlYear = document.getElementById("sYear");
var ddlMonth = document.getElementById("sMonth");
var ddlDate = document.getElementById("sDate");
var opt = null;
var curDay = new Date();
var year = curDay.getFullYear();
for (var i = -5; i < 5; i++)
{
opt = document.createElement("OPTION");
opt.value = year + i;
opt.innerText = (year + i) + "年";
ddlYear.appendChild(opt);
}
ddlYear.value = curDay.getFullYear();
for (var i = 1; i <= 12; i++)
{
opt = document.createElement("OPTION");
opt.value = i;
opt.innerText = i + "月";
ddlMonth.appendChild(opt);
}
ddlMonth.value = curDay.getMonth() + 1;
fillDate();
ddlDate.value = curDay.getDate();
}
function fillDate()
{
var ddlYear = document.getElementById("sYear");
var ddlMonth = document.getElementById("sMonth");
var ddlDate = document.getElementById("sDate");
while (ddlDate.options.length > 0)
{
ddlDate.remove(0);
}
var selDay = new Date();
selDay.setFullYear(ddlYear.value, ddlMonth.value, 1);
var lastDay = new Date(selDay - (24 * 60 * 60 * 1000));
var days = lastDay.getDate();
for (var i = 1; i <= days; i++)
{
var opt = document.createElement("OPTION");
opt.value = i;
opt.innerText = i + "日";
ddlDate.appendChild(opt);
}
}
</script>
</head>
<body onload="fillSelect()">
<select id="sYear" onchange="fillDate()"></select>
<select id="sMonth" onchange="fillDate()"></select>
<select id="sDate"></select>
</body>
</html>
展开全部
daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
然后根据你选择的月 来提取当月中的天数 在循环的创建
在选年的时候 在判断下一 是否是闰年。。更改一个 这个数组的二月
然后根据你选择的月 来提取当月中的天数 在循环的创建
在选年的时候 在判断下一 是否是闰年。。更改一个 这个数组的二月
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<html>
<head>
<style>
#sYear{width:65px;}
#sMonth{width:50px;}
#sDate{width:50px;}
</style>
<script>
functionfillSelect()
{
varddlYear=document.getElementById("sYear");
varddlMonth=document.getElementById("sMonth");
varddlDate=document.getElementById("sDate");
varopt=null;
varcurDay=newDate();
varyear=curDay.getFullYear();
for(vari=-5;i<5;i++)
{
opt=document.createElement("OPTION");
opt.value=year+i;
opt.innerText=(year+i)+"年";
ddlYear.appendChild(opt);
}
ddlYear.value=curDay.getFullYear();
for(vari=1;i<=12;i++)
{
opt=document.createElement("OPTION");
opt.value=i;
opt.innerText=i+"月";
ddlMonth.appendChild(opt);
}
ddlMonth.value=curDay.getMonth()+1;
fillDate();
ddlDate.value=curDay.getDate();
}
functionfillDate()
{
varddlYear=document.getElementById("sYear");
varddlMonth=document.getElementById("sMonth");
varddlDate=document.getElementById("sDate");
while(ddlDate.options.length>0)
{
ddlDate.remove(0);
}
varselDay=newDate();
selDay.setFullYear(ddlYear.value,ddlMonth.value,1);
varlastDay=newDate(selDay-(24*60*60*1000));
vardays=lastDay.getDate();
for(vari=1;i<=days;i++)
{
varopt=document.createElement("OPTION");
opt.value=i;
opt.innerText=i+"日";
ddlDate.appendChild(opt);
}
}
</script>
</head>
<bodyonload="fillSelect()">
<selectid="sYear"onchange="fillDate()"></select>
<selectid="sMonth"onchange="fillDate()"></select>
<selectid="sDate"></select>
</body>
</html>
<head>
<style>
#sYear{width:65px;}
#sMonth{width:50px;}
#sDate{width:50px;}
</style>
<script>
functionfillSelect()
{
varddlYear=document.getElementById("sYear");
varddlMonth=document.getElementById("sMonth");
varddlDate=document.getElementById("sDate");
varopt=null;
varcurDay=newDate();
varyear=curDay.getFullYear();
for(vari=-5;i<5;i++)
{
opt=document.createElement("OPTION");
opt.value=year+i;
opt.innerText=(year+i)+"年";
ddlYear.appendChild(opt);
}
ddlYear.value=curDay.getFullYear();
for(vari=1;i<=12;i++)
{
opt=document.createElement("OPTION");
opt.value=i;
opt.innerText=i+"月";
ddlMonth.appendChild(opt);
}
ddlMonth.value=curDay.getMonth()+1;
fillDate();
ddlDate.value=curDay.getDate();
}
functionfillDate()
{
varddlYear=document.getElementById("sYear");
varddlMonth=document.getElementById("sMonth");
varddlDate=document.getElementById("sDate");
while(ddlDate.options.length>0)
{
ddlDate.remove(0);
}
varselDay=newDate();
selDay.setFullYear(ddlYear.value,ddlMonth.value,1);
varlastDay=newDate(selDay-(24*60*60*1000));
vardays=lastDay.getDate();
for(vari=1;i<=days;i++)
{
varopt=document.createElement("OPTION");
opt.value=i;
opt.innerText=i+"日";
ddlDate.appendChild(opt);
}
}
</script>
</head>
<bodyonload="fillSelect()">
<selectid="sYear"onchange="fillDate()"></select>
<selectid="sMonth"onchange="fillDate()"></select>
<selectid="sDate"></select>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询