http://www.sayee.com/cloudchen/js/date.htm

解决方案 »

  1.   

    Date.prototype.getLastDay = function(year,month) {
        if(arguments.length==2)
    return(new Date(year,month,0).getDate())
    else
    with(new Date())return(new Date(getYear(),getMonth()+1,0).getDate())
    }
    //系统变量
    var i,j=0,iYearSelectedIndex,iDateSelectIndex;
    var now = new Date();
    with(now) var nowY=getYear(),nowM=getMonth()+1,nowD=getDate(),nowLD=getLastDay();
    //用户变量
    var startYear = 1949,endYear = 2049;
    var formYname="year",formMname="month",formDname="date";
    var sYearTip="年",sMonthTip="月",sDateTip="日";
    //创建到哪里
    var oWhere = document.forms[0];
    //创建所有的对象
    var elemYear = new Array(),elemMonth = new Array(),elemDate = new Array();
    with(document) {
    elemYear[0] = createElement("<SELECT name='"+formYname+"'>");
    elemYear[1] = createTextNode(sYearTip);
    elemMonth[0] = createElement("<SELECT name='"+formMname+"'>");
    elemMonth[1] = createTextNode(sMonthTip);
    elemDate[0] = createElement("<SELECT name='"+formDname+"'>");
    elemDate[1] = createTextNode(sDateTip);
    }
    //设置所有对象的属性
    //className,id
    with(elemYear[0])className="userData",id="Year";
    with(elemMonth[0])className="userData",id="Month";
    with(elemDate[0])className="userData",id="Date";
    //按索引顺序逐个添加对象
    appendAllChild(elemYear),appendAllChild(elemMonth),appendAllChild(elemDate);
    //创建列表框内容
    CreateYearOptions()
    CreateMonthOptions();
    CreateDateOptions(nowLD,nowD-1);
    //触发onchange事件
    elemYear[0].onchange = function() {CreateDateOptions(now.getLastDay(this.value,elemMonth[0].value))}
    elemMonth[0].onchange = function() {CreateDateOptions(now.getLastDay(elemYear[0].value,this.value))}
    elemDate[0].onchange = function() {iDateSelectIndex=this.selectedIndex}
    /* FUNCTION */
    //append Child function
    function appendAllChild(elem) {
    with(oWhere) {
    for (var i=0;i<elem.length;i++)
    appendChild(elem[i]);
    }
    }
    //create options of year
    function CreateYearOptions() {
    with(elemYear[0]) {
    for (i=startYear;i<=endYear;i++) {
    options[j] = new Option(i,i);
    j++;
    }
    iYearSelectedIndex = nowY-startYear
    options[iYearSelectedIndex].selected=true;
    }
    }
    //create options of month
    function CreateMonthOptions() {
    with(elemMonth[0]) {
    for (i=0;i<12;i++)options[i] = new Option(i+1,i+1);
    options[nowM-1].selected=true;
    }
    }
    //create options of date
    function CreateDateOptions(lastday,selectToday) {
    with(elemDate[0]) {
    length=0;
    for (i=0;i<lastday;i++)options[i] = new Option(i+1,i+1)
    if (selectToday) {
    options[selectToday].selected=true,iDateSelectIndex=selectToday;
    } else {
    iDateSelectIndex = Math.min(iDateSelectIndex,lastday-1);
    }
    options[iDateSelectIndex].selected=true;
    }
    }
    /*记录用户选择状态,以下内容可以删除!*/
    document.body.onload = function() {LoadUserData()};
    document.body.onunload = function() {SaveUserData();}//关闭窗口时Save
    document.body.onbeforeunload = function() {SaveUserData();}//跳到其他URL时Save
    document.all.permit.onclick = function () {
    with(document.all.permit) {
    setAttribute("auto",checked);
    save("userAutoSaveLoad");
    }
    }function SaveUserData() {
    if(LoadAuto()||arguments.length) {
    saveFunction(document.all.Year);
    saveFunction(document.all.Month);
    saveFunction(document.all.Date);
    }
    }
    function LoadUserData() {
    if(LoadAuto()||arguments.length) {
    LoadFunction(document.all.Year);
    LoadFunction(document.all.Month);
    LoadFunction(document.all.Date);
    iDateSelectIndex = document.all.Date.selectedIndex;
    }
    }
    function ClearUserData() {
    with(document.all) {
    ClearFunction(Year,iYearSelectedIndex)
    ClearFunction(Month,nowM-1)
    ClearFunction(Date,nowD-1)
    }
    }
    function saveFunction(elem) {
    with(elem) {
    setAttribute("userSelect",selectedIndex);
    save("userSelectedIndex"+id);
    }
    }
    function LoadFunction(elem) {
    with(elem) {
    load("userSelectedIndex"+id);
    iIndex = getAttribute("userSelect");
    if(iIndex!=null)selectedIndex=iIndex;
    }
    }
    function ClearFunction(elem,iIndex) {
    with(elem) {
    removeAttribute("userSelect");
    save("userSelectedIndex"+id);
    options[iIndex].selected=true;
    }
    }
    function LoadAuto() {
    with(document.all.permit) {
    load("userAutoSaveLoad");
    if(getAttribute("auto")=="true")
    checked=true;
    else
    checked=false;
    return(checked)
    }
    }
    /*Show Value*/
    function showValue() {
    alert(document.forms[0][formYname].value+"-"+document.forms[0][formMname].value+"-"+document.forms[0][formDname].value)
    }