添加YYYY,MM,DD下拉列表以选择网页上的日期

时间:2011-07-18 20:41:45

标签: javascript html

我想添加下拉样式日期选择器,根据所选年份和月份重新填充日期列表,例如。 2008年2月有29天,4月有30天,6月有31天等...我怎么能用javascript做到这一点?

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

这是你在寻找什么?

http://keith-wood.name/datepick.html

答案 2 :(得分:1)

<强> HTML

<select name="" id="year">
    <option value="2016">2016</option>
    <option value="2015">2015</option>
    <option value="2014">2014</option>
    <option value="2013">2013</option>
    <option value="2012">2012</option>
</select>
<select name="" id="month">
    <option value="0">January</option>
    <option value="1">February</option>
    <option value="2">March</option>
    <option value="3">April</option>
    <option value="4">May</option>
    <option value="5">June</option>
    <option value="6">July</option>
    <option value="7">August</option>
    <option value="8">September</option>
    <option value="9">October</option>
    <option value="10">Novenmer</option>
    <option value="11">December</option>
</select>
<select name="" id="day">

</select>    

<强> SCRIPT

<script>
        $(document).ready(function(){

            dpick('#year','#month','#day',1);
        });

        function dpick(year,month,day,td){
            if(td==1){
                var today = new Date();
                $(year).val(today.getFullYear());
                $(month).val(today.getMonth());
            }
            $(year).change(function(){
                dpick(year,month,day);
            });
            $(month).change(function(){
                dpick(year,month,day);
            }); 
            var month_val = $(month).val();
            var i=0;
            $(day).empty();
            if(month_val=="1"){
                if($(year).val()%4 == 0){
                    for(i=1;i<=29;i++){
                        $(day).append($("<option></option>").attr("value", i).text(i));
                    }
                }else{
                    for(i=1;i<=28;i++){
                        $(day).append($("<option></option>").attr("value", i).text(i));
                    }
                }
            }
            else if(month_val=="8" || month_val=="3" || month_val=="5" || month_val=="10"){
                for(i=1;i<=30;i++){
                    $(day).append($("<option></option>").attr("value", i).text(i));
                }
            }
            else{
                for(i=1;i<=31;i++){
                    $(day).append($("<option></option>").attr("value", i).text(i));
                }
            }
            if(td==1){
                $(day).val(today.getDate());
            }
        }
    </script>