admin管理员组

文章数量:1323157

I want to add the dropdown style date selector which repopulates the date list according to the selected year and month eg. february 2008 has 29 days, april has 30 days while june has 31 days etc... How can i do this using javascript?

I want to add the dropdown style date selector which repopulates the date list according to the selected year and month eg. february 2008 has 29 days, april has 30 days while june has 31 days etc... How can i do this using javascript?

Share edited Jul 18, 2011 at 21:22 nilakshdas asked Jul 18, 2011 at 20:41 nilakshdasnilakshdas 1151 gold badge1 silver badge8 bronze badges 1
  • yes, to select one's date of birth... – nilakshdas Commented Jul 18, 2011 at 20:44
Add a ment  | 

3 Answers 3

Reset to default 3

This might help

http://javascript.internet./forms/date-selection-form.html

I would remend to use

http://jqueryui./demos/datepicker/

Is this what your searching for ?

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

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>

本文标签: