原生js循环出年月选择框

j这里只写一下js

 //循环出年月
        var now = new Date();
        var currentYear = now.getFullYear();
        var currentMonth = now.getMonth() + 1;
        BindSelect(document.getElementById('year'), currentYear - 50, currentYear + 10, currentYear);
        BindSelect(document.getElementById('month'), 1, 12, currentMonth);
        BindSelect(document.getElementById('end-month'), 1, 12, currentMonth);
        function BindSelect(selectObj, startValue, endValue, selectedValue){
            var i = 0;
            for(var val = startValue; val <= endValue; val++)
            {
           //这里判断是为了加上年和月单位
            if(val>12){
                selectObj.options.add(new Option(val+'年' , val));
            }else{
                selectObj.options.add(new Option(val+'月' , val));
            }
            if(val == selectedValue) 
              selectObj.options[i+1].selected = true;
              i++;
            }
            }

你可能感兴趣的:(原生js循环出年月选择框)