Javascript语言用3个列表框(select)实现的年、月、日三级联动

本文介绍了一个使用HTML和JavaScript实现的简单日期选择器。该选择器可根据用户选定的年份和月份动态更新对应的天数选项。

以下是代码:

<HTML>
<HEAD>
<TITLE>日历</TITLE>
</HEAD>
<BODY onload="load()">
<SCRIPT LANGUAGE="JavaScript">
function TGetDaysInMonth(iMonth, iYear) {
  var dPrevDate = new Date(iYear, iMonth, 0);
  return dPrevDate.getDate();
}

function TUpdateCal(iYear, iMonth) {
  var dDate=new Date();
  daysInMonth = TGetDaysInMonth(iMonth, iYear);
  while(document.all.tbSelDay.childNodes.length>0)
  {
    document.all.tbSelDay.removeChild(document.all.tbSelDay.childNodes[0]);
  }

  for (d = 1; d <= parseInt(daysInMonth); d++) {
 option=document.createElement("option");
 option.value=d;
 option.appendChild(document.createTextNode(d));
 document.all.tbSelDay.appendChild(option);
 if(d==dDate.getDate())
 {
  option.selected=true;
 }
  }
}

function load()
{
  <!--出始化年-->
  var dDate = new Date();
  var dCurYear = dDate.getFullYear();
  for(var i=dCurYear-10;i<dCurYear+5;i++)
  {
    option=document.createElement("option");
    option.value=i;
    option.appendChild(document.createTextNode(i));
    document.all.tbSelYear.appendChild(option);
    if(i==dCurYear)
    {
 option.selected=true;
    }
  }
  <!--出始化月-->
  for(var i=1;i<=12;i++)
  {
    option=document.createElement("option");
    option.value=i;
    option.appendChild(document.createTextNode(i));
    document.all.tbSelMonth.appendChild(option);
    if(i==(dDate.getMonth()+1))
    {
        option.selected=true;
    }
  }
}
</script>

<form name="frmCalendarSample" method="post" action="">
年:
<select name="tbSelYear" onchange='TUpdateCal(frmCalendarSample.tbSelYear.value, frmCalendarSample.tbSelMonth.value)'>
</select>
月:
<select name="tbSelMonth" style="width: 50px" onchange='TUpdateCal(frmCalendarSample.tbSelYear.value, frmCalendarSample.tbSelMonth.value)'>
</select>
日:
<select name="tbSelDay"></select>
<script language="JavaScript">
TUpdateCal(frmCalendarSample.tbSelYear.value, frmCalendarSample.tbSelMonth.value);
</script>
</form>
</BODY>
</HTML>

 

你也可以直接下载:

http://cn.ziddu.com/download/264275/date.txt.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值