첫번째 함수인 createDateSelectBox를 약간만 수정하면 현재년도에서 플러스 몇년 또는 마이너스 몇년의 단일 기능만 하는 함수로도 활용이 가능하다. (먼저 단일 기능을 먼저 만들었었는데..까먹었다..)

function createDateSelectBox(name, num) {
 // 파라미터 설명 : name - 셀렌트 박스 이름, num - 현재년도에서 플러스, 마이너스 년도
 
 var dt = new Date();
 var c_id = document.getElementById(name + "_id");
 var inHtml = "";
 var currentYear = dt.getFullYear();
 var currentMonth = dt.getMonth() + 1;
 var currentDay = dt.getDate();
 var dt2 = new Date(currentYear, currentMonth, "");
 var intNum = parseInt(num);
 var optMonth = "";
 var optDay = "";
 var lastDay = dt2.getDate();
 //var selected = "";
 
 inHtml += " <select name='" + name + "_year' id='" + name + "_year' onchange='onchageDate(\"" + name + "\")'> ";  
 inHtml += " <option value='' >선택</option>";
 for (var i = currentYear - intNum; i <= currentYear + intNum; i++) {
  inHtml += "<option value='" + i + "' >" + i + "</opton>";
 }
 inHtml += " </select> ";
 
 inHtml += " <select name='" + name + "_month' id='" + name + "_month' onchange='onchageDate(\"" + name + "\")'> ";
 inHtml += " <option value='' >선택</option>";
 for (var i = 1; i <= 12; i++) {
  if (i >= 1 && i < 10) {
   optMonth = "0" + i;
  } else {
   optMonth = i;
  }
 
  inHtml += " <option value='" + optMonth + "' >" + optMonth + "</option>";
 }
 inHtml += " </select> ";
 
 inHtml += " <select name='" + name + "_day' id='" + name + "_day'> ";
 inHtml += " <option value='' >선택</option>";
 for (var i = 1; i <= lastDay; i++) {
  if (i >= 1 && i < 10) {
   optDay = "0" + i;
  } else {
   optDay = i;
  }
 
  inHtml += " <option value='" + optDay + "' >" + optDay + "</option>";
 }
 inHtml += " </select> ";
 
 
 c_id.innerHTML = inHtml;
}

function onchageDate(name) {
 var optDay = "";
 // year, month, day SelectBox찾기
    selectYear = document.getElementById(name+"_year");
    selectMonth = document.getElementById(name+"_month");
    selectDay = document.getElementById(name+"_day");
   
    // 현재 년도와 월 구하기
    year = selectYear.options[selectYear.selectedIndex].value;
    month = selectMonth.options[selectMonth.selectedIndex].value;
    
 tmpDate = new Date(year, month, 0);
 
    selectedIndex = selectDay.selectedIndex;
   
    for(i = selectDay.length-1; i >= 0; i--) {
     selectDay.options[i] = null;
    }
   
    selectDay.options[0] = new Option('선택', '');
   
    for(i = 1; i <= tmpDate.getDate(); i++) {
     if (i >= 1 && i < 10) {
   optDay = "0" + i;
  } else {
   optDay = i;
  }
     selectDay.options[i] = new Option(optDay, optDay);
    }
   
    if(selectedIndex <= tmpDate.getDate()) {
     selectDay.options[selectedIndex].selected = true;
    } else {
     selectDay.options[tmpDate.getDate()-1].selected = true;
    }
}



 
html 에 소스에는 아래와 같이 활용하면 된다.

<span style="border: 0px;" id="cntc_dat_id"><script type="text/javascript">createDateSelectBox('cntc_dat', 5);</script></span>

Posted by 카스카

댓글을 달아주세요:: 네티켓은 기본, 스팸은 사절

  1. 2009/01/03 09:55
    댓글 주소 수정/삭제 댓글
    랜덤타고 들어왔는데 첫글이... 깜짝놀랐어요~
    멋지세요~ 이런걸!!!!

◀ PREV : [1] : [2] : [3] : [4] : [5] : ... [80] : NEXT ▶

BLOG main image

공지사항

분류
분류 전체보기 (80)
게임 (2)
영화 (3)
직딩일기 (4)
컴퓨터 (35)
일상 (9)
쓸데없는생각들 (5)
책읽기 (9)
음악 (0)
글 보관함
달력
«   2009/07   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Total : 65,000
Today : 0 Yesterday : 10
tistory