js 怎么循环出一个关于日期的select框
1楼下面这样3个年-月-日的select框,怎么根据月份循环出天数,比如当前是润年2月,天数就有29天,平时28,这样。求高手帮忙,非常感谢!<selectname='y...
1楼
下面这样3个年-月-日的select框,怎么根据月份循环出天数,比如当前是润年2月,天数就有29天,平时28,这样。
求高手帮忙,非常感谢!
<select name = 'year'>
</select>
<select name = 'month'>
</select>
<select name = 'day'>
</select> 展开
下面这样3个年-月-日的select框,怎么根据月份循环出天数,比如当前是润年2月,天数就有29天,平时28,这样。
求高手帮忙,非常感谢!
<select name = 'year'>
</select>
<select name = 'month'>
</select>
<select name = 'day'>
</select> 展开
4个回答
展开全部
给一个代码例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="d_reg_form" id="d_reg_form" action="">
<select name="year" size="1" onChange="return CheckDay();">
<script language="javascript">
for(i=2050;i>=1940;i--)
{
document.write('<option value="'+i+'">'+i+'</option>')
}
</script>
</select>年
<select name="month" size="1" onChange="return CheckDay();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>月
<select name="day" size="1"></select>日
<script>
function CheckDay(){
var YearNo,MonthNo,DayNo;
YearNo = document.d_reg_form.year.value;
MonthNo = document.d_reg_form.month.value;
var i,j,DayCount;
if(MonthNo==1 || MonthNo==3 || MonthNo==5 || MonthNo==7 || MonthNo==8 || MonthNo==10 || MonthNo==12)
{DayCount = 31;}
if(MonthNo==4 || MonthNo==6 || MonthNo==9 || MonthNo==11 )
{DayCount = 30;}
if(MonthNo==2){
if(IsRunY(YearNo)){
DayCount = 29;
}else{
DayCount = 28;
}
}
document.d_reg_form.day.length=DayCount;
for (i=0;i<=DayCount-1;i++)
{
document.d_reg_form.day[i]=new Option(i+1);
document.d_reg_form.day[i].value=i+1;
}
}
function IsRunY(YearNo)
{
// 能被4整除却不能被100整除 或能被400整除的年份
// 则是闰年
if ((YearNo % 4 == 0 && YearNo % 100 != 0) || YearNo % 400 == 0){
return true;
}else{
return false;
}
}
CheckDay();
</script>
</form>
</body>
</html>
展开全部
这样很麻烦的,而且需要联动,如果你日期只输出28天,那选择2月以外的月份时还得刷新日期列表.
有两个简单的办法,一是找一个JS的日期选择器,方便好用.二是天数输出31天,但在提交前用JS检查日期的合法性,如果日期非法就给出提示.验证日期合法性也比较简单,正则,或是在try块中强制类型转换,如果出错就是非法日期.
有两个简单的办法,一是找一个JS的日期选择器,方便好用.二是天数输出31天,但在提交前用JS检查日期的合法性,如果日期非法就给出提示.验证日期合法性也比较简单,正则,或是在try块中强制类型转换,如果出错就是非法日期.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<html>
<head>
<title>添加行</title>
<script type="text/javascript">
var yearState;
var monthState;
var dayState;
var sltYear;
var sltMonth;
var sltDay;
function InitDate(cid, dateNow) { //cid:控件容器ID,dataNow:默认时间,只支持2012-3-13或2012/3/13,没有出错处理
document.getElementById(cid).innerHTML = '<select id="sltYear" onchange="yearChange(this.value)"><option
value="2011">2011</option><option value="2012">2012</option></select><select id="sltMonth" onchange="monthChange
(this.value)"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option
value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option
value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option
value="12">12</option></select><select id="sltDay""><option value="1">1</option><option value="2">2</option><option
value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option
value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option
value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option
value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option
value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option
value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option
value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option
value="31">31</option></select>';
sltYear = document.getElementById("sltYear");
sltMonth = document.getElementById("sltMonth");
sltDay = document.getElementById("sltDay");
yearChange(sltYear.value);
setDate(dateNow);
}
//年改变事件
function yearChange(yv) {
var v = parseInt(yv);
if (v % 400 == 0 || (v % 100 != 0 && v % 4 == 0)) {//闰年判断
yearState = 1;
} else {
yearState = 0;
}
monthChange(sltMonth.value);
}
//月改变时间==事件
function monthChange(mv) {
switch (mv) {
case "1":
case "3":
case "5":
case "7":
case "8":
case "10":
case "12":
monthState = 1;
break;
case "4":
case "6":
case "9":
case "11":
monthState = 2;
break;
case "2":
monthState = 3;
if (yearState == 1) {
monthState = 4;
}
break;
}
dayChange(sltDay);
switch (monthState) {
case 1:
updateDay(1, 31);
break;
case 2:
updateDay(0, 30);
updateDay(1, 30);
break;
case 3:
updateDay(0, 28);
updateDay(1, 28);
break;
case 4:
updateDay(0, 29);
updateDay(1, 29);
break;
}
}
//处理月修改时,当月不存在该日期
function dayChange(d) {
var v = parseInt(d.value);
if (monthState == 1 || v < 29) {
return;
}
switch (monthState) {
case 2:
if (v == 31) {
d.value = "30";
}
break;
case 3:
if (v > 28) {
d.value = "28";
}
break;
case 4:
if (v > 29) {
d.value = "29";
}
break;
}
}
//修改日的日期数
function updateDay(t, e) {//e代表日期数
var l;
if (t == 1) {//增加项
while (sltDay.options.length < e) {
l = sltDay.options.length;
sltDay.options[l] = new Option(l + 1, l + 1);
}
} else {//减少项
while (sltDay.options.length > e) {
l = sltDay.options.length;
sltDay.options[l - 1] = null;
}
}
}
///赋值日期
function setDate(value) {
if (value != null && value != "") {
var date = new Date(value.replace(/\-/g, "/"));
sltYear.value = date.getFullYear();
sltMonth.value = date.getMonth() + 1;
sltDay.value = date.getDate();
}
}
function getDayValue() {
return sltDay.value;
}
function getMonthValue() {
return sltMonth.value;
}
function getYearValue() {
return sltYear.value;
}
function getDateValue() {
return sltYear.value + "-" + sltMonth.value + "-" + sltDay.value;
}
window.onload = function () {
InitDate("resultDiv","2012-3-13");
}
</script>
</head>
<body>
<div id="resultDiv">
</div>
</body>
</html>
<head>
<title>添加行</title>
<script type="text/javascript">
var yearState;
var monthState;
var dayState;
var sltYear;
var sltMonth;
var sltDay;
function InitDate(cid, dateNow) { //cid:控件容器ID,dataNow:默认时间,只支持2012-3-13或2012/3/13,没有出错处理
document.getElementById(cid).innerHTML = '<select id="sltYear" onchange="yearChange(this.value)"><option
value="2011">2011</option><option value="2012">2012</option></select><select id="sltMonth" onchange="monthChange
(this.value)"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option
value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option
value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option
value="12">12</option></select><select id="sltDay""><option value="1">1</option><option value="2">2</option><option
value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option
value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option
value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option
value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option
value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option
value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option
value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option
value="31">31</option></select>';
sltYear = document.getElementById("sltYear");
sltMonth = document.getElementById("sltMonth");
sltDay = document.getElementById("sltDay");
yearChange(sltYear.value);
setDate(dateNow);
}
//年改变事件
function yearChange(yv) {
var v = parseInt(yv);
if (v % 400 == 0 || (v % 100 != 0 && v % 4 == 0)) {//闰年判断
yearState = 1;
} else {
yearState = 0;
}
monthChange(sltMonth.value);
}
//月改变时间==事件
function monthChange(mv) {
switch (mv) {
case "1":
case "3":
case "5":
case "7":
case "8":
case "10":
case "12":
monthState = 1;
break;
case "4":
case "6":
case "9":
case "11":
monthState = 2;
break;
case "2":
monthState = 3;
if (yearState == 1) {
monthState = 4;
}
break;
}
dayChange(sltDay);
switch (monthState) {
case 1:
updateDay(1, 31);
break;
case 2:
updateDay(0, 30);
updateDay(1, 30);
break;
case 3:
updateDay(0, 28);
updateDay(1, 28);
break;
case 4:
updateDay(0, 29);
updateDay(1, 29);
break;
}
}
//处理月修改时,当月不存在该日期
function dayChange(d) {
var v = parseInt(d.value);
if (monthState == 1 || v < 29) {
return;
}
switch (monthState) {
case 2:
if (v == 31) {
d.value = "30";
}
break;
case 3:
if (v > 28) {
d.value = "28";
}
break;
case 4:
if (v > 29) {
d.value = "29";
}
break;
}
}
//修改日的日期数
function updateDay(t, e) {//e代表日期数
var l;
if (t == 1) {//增加项
while (sltDay.options.length < e) {
l = sltDay.options.length;
sltDay.options[l] = new Option(l + 1, l + 1);
}
} else {//减少项
while (sltDay.options.length > e) {
l = sltDay.options.length;
sltDay.options[l - 1] = null;
}
}
}
///赋值日期
function setDate(value) {
if (value != null && value != "") {
var date = new Date(value.replace(/\-/g, "/"));
sltYear.value = date.getFullYear();
sltMonth.value = date.getMonth() + 1;
sltDay.value = date.getDate();
}
}
function getDayValue() {
return sltDay.value;
}
function getMonthValue() {
return sltMonth.value;
}
function getYearValue() {
return sltYear.value;
}
function getDateValue() {
return sltYear.value + "-" + sltMonth.value + "-" + sltDay.value;
}
window.onload = function () {
InitDate("resultDiv","2012-3-13");
}
</script>
</head>
<body>
<div id="resultDiv">
</div>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还是找个日历插件吧,简单方便!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询