开发者

Form days value change with respect to the month

开发者 https://www.devze.com 2023-02-15 08:11 出处:网络
How can I create a day drop-down that shows the correct number of days depending on theMonth selected in other drop-down?

How can I create a day drop-down that shows the correct number of days depending on the Month selected in other drop-down?

I dont want the user to enter a date of birth.

Can this be done with any bu开发者_如何学编程ilt in functions in the Javascript or Php Dynamically?

Or do I have to write separate functions for each month?


No, there are no built-in Javascript or PHP functions to dynamically manipulate an HTML "day in month" dropdown.

However, there are date functions in both languages (PHP · Javascript) that can tell you how many days are in a given month.

I recommend using Javascript to manipulate the children of your "day" <select /> element accordingly, when there is a change in selection in your "month" <select /> element.

Or save yourself the trouble and use the datejs library.

Good luck!


1 to 28 are common for all months. for 29 onwards we have to make choice.

787:

addOption 29 when feb and leapyear
addOptions 29 30 when month with 30 days april,june,sep,nov
addOptions 29 30 31 when months with 31 days   jan,march,may,july,aug,oct,dec

if clicked second time remove all added options and then re check the month and year value to find the category in which the month lies GOTO 787

<script type="text/javascript">
        var monthselected,yearselected,mtype=0,visited=0;

        function removeOptions(){  //removes options call each time the day selector is clicked 
        var x=document.getElementById('dayselect');
        while(x.length>29)
            { x.remove(x.length-1);}
            }

        function addOptions(mtype)
        {                              //adds options acc to mtype
        //alert('initialising');
        var i;
        var x=document.getElementById("dayselect");
        for(i=29;i<=mtype;i++)
        {   var option=document.createElement("option");

        option.text=i;
        try
          {//alert('trying');
          // for IE earlier than version 8
          x.add(option,x.options[null]);
          }
        catch (e)
          {//alert('catching');
          x.add(option,null);
          }
        }
        }                           
        function isleapyear(year){ //find if year is leap or not
                if((year%4)==0)
                {
                if((year%100)!=0)
                {
                return true;
                }
                else return false;
                }
                if((year%400)==0)
                    {

                    return true;

                    }

        else return false;
        }                   




        </script>




<html><td id="bday" >Birthday</td>
<td><select  id="month" name="month" class="int" onBlur="monthselected=document.getElementById('month').value;">
                            <option value="00">Month</option>
                            <option value="01" >January</option>
                            <option value="02" >  February</option>
                            <option value="03" >  March</option>
                            <option value="04" >  April</option>
                            <option value="05" >  May</option>
                            <option value="06" >  June</option>
                            <option value="07" >  July</option>
                            <option value="08" >  August</option>
                            <option value="09" >  September</option>
                            <option value="10" >  October</option>
                            <option value="11" >  November</option>
                            <option value="12" >  December</option>
                            </select>

    <label id="year-label" class="year int">Year<select class="int" id="year" name="year" selected= onBlur="yearselected=document.getElementById('year').value;"><option value="0" >YYYY</option>
   <?php for($i=2012;$i>1912;$i--){echo "<option value=\"$i\">$i</option>";} ?>
                                                                                </select>

 <label id="day-label" class="day int">Day<select id="dayselect" class="int" name="day" onClick=
"if(monthselected==4||monthselected==6||monthselected==9||monthselected==11)
                                {mtype=30;}
                            if(monthselected==2&&isleapyear(yearselected))
                                {mtype=29;}
                            if(monthselected==2&&!isleapyear(yearselected))
                                {mtype=28;}

else if(mtype==0){mtype=31;} 

if(visited!=0){removeOptions();}
   addOptions(mtype);
   visited=1;">
 <option value="0" >  DD</option><?php for($i=1;$i<29;$i++){
                        echo "<option value=\"$i\">$i</option>";} ?>
  </select>

   </label>


   </td>

this is what i was working on today. the appropriate nodes are being added or removed on onClick event.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号