네이티브 JS 구현 날짜 선택

12504 단어
JS 선언 방법
/*
*
* sel_year、sel_month、sel_day
*/ var birthday1 = { date_picker: function(options){ var defaults = { YearSelector: "#sel_year", MonthSelector: "#sel_month", DaySelector: "#sel_day", FirstText: "", FirstValue: 0 }; var opts = $.extend({}, defaults, options); var $YearSelector = $(opts.YearSelector); var $MonthSelector = $(opts.MonthSelector); var $DaySelector = $(opts.DaySelector); var FirstText = opts.FirstText; var FirstValue = opts.FirstValue; // var str = ""; $YearSelector.html(str); $MonthSelector.html(str); $DaySelector.html(str); // var yearNow = new Date().getFullYear(); var yearSel = $YearSelector.attr("rel"); for (var i = yearNow; i >= 1900; i--) { var sed = yearSel==i?"selected":""; var yearStr = ""; $YearSelector.append(yearStr); } // var monthSel = $MonthSelector.attr("rel"); for (var i = 1; i <= 12; i++) { var monthStr=null; var sed = monthSel==i?"selected":""; if(i<10){ monthStr=""; }else{ monthStr = ""; } $MonthSelector.append(monthStr); } // ( ) function BuildDay() { if ($YearSelector.val() == 0 || $MonthSelector.val() == 0) { // $DaySelector.html(str); } else { $DaySelector.html(str); var year = parseInt($YearSelector.val()); var month = parseInt($MonthSelector.val()); var dayCount = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: dayCount = 31; break; case 4: case 6: case 9: case 11: dayCount = 30; break; case 2: dayCount = 28; if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { dayCount = 29; } break; default: break; } var daySel = $DaySelector.attr("rel"); for (var i = 1; i <= dayCount; i++) { var dayStr=null; var sed = daySel==i?"selected":""; if(i<10){ dayStr = ""; // dayStr =`` }else{ dayStr = ""; // dayStr =`` } $DaySelector.append(dayStr); } } } $MonthSelector.change(function () { BuildDay(); }); $YearSelector.change(function () { BuildDay(); }); if($DaySelector.attr("rel")!=""){ BuildDay(); } } }

HTML
<select id="year" rel="2020">select>  
<select id="month" rel="05">select>  
<select id="day" rel="12">select>  

JS 호출
$(function() {
    // birthday.date_picker({ YearSelector:
"#year", MonthSelector: "#month", DaySelector: "#day", });       
    // birchange(
"year"); birchange("month"); birchange("day"); }


드롭다운 상자 클릭 이벤트
    function birchange(field){
        $("#"+field).change(function(){
            $.ajax({
                url:"../php/zyf.php",
                data:{type:"birthday",num:field,val:$(this).val()},
                success:function(res){
                    if(res == "ok"){
                        layer.msg(' ');
                    }
                    window.location.reload();
                }
            })
        })
    }

백그라운드는 바로 표 수신 필드이므로 추태를 보이지 않는다

좋은 웹페이지 즐겨찾기