2011-03-08 3 views
1

다른 드롭 다운에서 선택한 월에 따라 올바른 일 수를 표시하는 일 드롭 다운을 어떻게 만들 수 있습니까?월에 대한 양식 일 값 변경

생년월일을 입력하고 싶지 않습니다.

Javascript 또는 PHP의 내장 함수로 동적으로 수행 할 수 있습니까?

또는 매월 별도의 기능을 작성해야합니까?

+0

나는 당신이 원하는 것을 명확하게하기 위해 부분을 바꾸는 자유를했습니다. 의도하지 않은 경우 –

+0

을 시도해보십시오. http://www.datejs.com/ 시도해보십시오. – Sotiris

+2

개인적으로 데이터 번호를 입력하는 것과 같은 단순한 무언가를위한 드롭 다운 메뉴 - 간단한 텍스트 필드와 유효성 검사 만하는 것이 아닌가요? 사용자가 입력 한 번호? –

답변

3

아니요, HTML "요일"드롭 다운을 동적으로 조작하는 JavaScript 또는 PHP 기능이 내장되어 있지 않습니다.

그러나 주어진 달에 몇 일이 있는지 알려주는 두 언어 (PHP · Javascript)의 날짜 기능이 있습니다.

나는 당신의 "달"<select /> 요소의 선택 범위에서의 변경이있을 때 당신의 "일"<select /> 요소 accordingly의 아이들을 manipulate 자바 스크립트를 사용하는 것이 좋습니다.

또는 문제를 해결하고 datejs 라이브러리를 사용하십시오.

행운을 빈다.

0

1에서 28까지는 모든 달에 공통적입니다. 29 이후에 을 선택해야합니다.

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 

클릭 두 번째 시간은 달이가 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> 

놓여있는 범주를 찾을 수있는 월과 연도 값을 확인 모든 추가 옵션을 제거한 다음 다시 경우 내가 오늘 일하고 있었던 일. onClick 이벤트에서 적절한 노드가 추가 또는 제거되고 있습니다.

+0

가끔은 약간의 버그가 있습니다. 필요에 따라 변경되지 않습니다 – avck

+0

이것이 해결책을줍니다. http://stackoverflow.com/a/14084904/1643143 – avck