年月日的三级联动

<input type="text" id="hs"> <input type="button" id="btn" value="提交">
<span id="rent"></span>

这个年月日的三级联动 主要是用的select标签

var str = "<select id='year'></select>&nbsp;&nbsp;<select id='month'>" +
        "</select>&nbsp;&nbsp;<select id='day'></select>";
    $("#rent").html(str);

    fullyear();
    fullmonth();
    fullday();

    //当其中一个改变,后面的要跟着改变

    $("#year").change(function(){

        fullday();
    });
    $("#month").change(function(){

        fullday();
    });


    function fullyear()

    {
        var d = new Date();
        var year = d.getFullYear();
        str ="";
        for(var i=year-5;i<year+6;i++)
        {
            if(i==year)
            {
                str += "<option selected='selected' value='"+i+"'>"+i+"</option>";
            }
            else {
                str +="<option value='"+i+"'>"+i+"</option>"
            }
        }
        $("#year").html(str);
    }

    function  fullmonth()
    {
        var d = new Date();
        var month = d.getMonth()+1;
        str ="";
        for(var j=1;j<13;j++)
        {
            if(j==month)
            {
                str += "<option selected='selected' value='"+j+"'>"+j+"</option>";
            }
            else {
                str +="<option value='"+j+"'>"+j+"</option>"
            }
        }
        $("#month").html(str);

    }

    function fullday()
    {
        var d = new Date();
        var day = d.getDate();
        var year=$("#year").val();
        var month=$("#month").val();
        var rq=31;
        str ="";
        if(month==4|| month==6|| month==9|| month===11)
        {
            rq=30;
        }
        else if(month==2)
        {
            if(year%4==0 && year%100!=0 || year%400==0)
            {
                rq=29;//闰年
            }
            else{
                rq=28; //不是闰年
            }
        }
        for(var n=1;n<rq+1;n++)
        {
            if(n==day)
            {
                str +="<option selected='selected' value='"+n+"'>"+n+"</option>";
            }
            else
            {
                str +="<option value='"+n+"'>"+n+"</option>";
            }
        }

        $("#day").html(str);

    } //到这里就完成了下拉列表的内容了,下一步要做的是把内容存到表单中
$("#btn").click(function(){
  var nian=$("#year").val();
  var yue=$("#month").val();
  var ri=$("#day").val();

var time=nian+"-"+yue+"-"+ri+"";

 $("#hs").val(time)


})

 

posted on 2017-05-19 11:49  霸道小豆丁  阅读(323)  评论(2编辑  收藏  举报

导航