how to restrict future month using javascript

前端 未结 2 568
北恋
北恋 2021-01-28 12:14

I\'m using two dropdown(month, year). I\'ve to restrict future month based on year. suppose if I choose \'2014\', it should show the months till January.I don\'t have any clue h

2条回答
  •  伪装坚强ぢ
    2021-01-28 13:08

    try this way

     $('#year').on('change',function() {
        var currMon= (new Date).getMonth();
        var currYear = (new Date).getFullYear();
    
        if(currYear === $('#year :selected').text()){
           $('#month option:eq('+currMon+')').prop('selected', true);
           $('#month').prop('disabled',true); 
        }  
        else{
           $('#month').prop('disabled',false); 
        }   
     });
    

    Note: the value of the month select should be 0 to 11 representing jan to dec

    Happy Coding :)

提交回复
热议问题