日期联动

var year=document.getElementById('year');
var month=document.getElementById('month');
var date=document.getElementById('date');
//获取日期
function getDaysOfMonth(year,month){
    var helpDate=new Date(year,month,0);
    return helpDate.getDate();
}
//清空select信息
function removeOpt(select){
    for(var i=select.length-1;i>0;i--){
        select.remove(i);
    }
}
//填充日期
function fillDate(dateSelect,year,month){
    var dateOfMonth=getDaysOfMonth(year,month);
    for(var i=1;i<=dateOfMonth;i++){
        var option=new Option(i,i);
        dateSelect.add(option);
    }
}
//填充月份
function fillMonth(monthSelect){
    for(var i=1;i<=12;i++){
        var option=new Option(i,i);
        monthSelect.add(option);
    }
}
//年份选项改变时
year.onchange=function(){
    var yearValue=year.value;
    var monthValue=month.value;
    removeOpt(month);
    removeOpt(date);
    if(yearValue!='0'){
        fillMonth(month);
    }
}
//月份选项改变时
month.onchange=function(){
    var yearValue=year.value;
    var monthValue=month.value;
    removeOpt(date);
    if(monthValue!='0'){
        fillDate(date,yearValue,monthValue);
    }
}

 

posted @ 2017-10-12 15:39  叶青山  阅读(222)  评论(0编辑  收藏  举报