how to restrict future month using javascript

前端 未结 2 569
北恋
北恋 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 :)

    0 讨论(0)
  • 2021-01-28 13:08

    $('#year').on('change',function() {

    var option=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"];

    var currMon= (new Date).getMonth();

    var currYear = (new Date).getFullYear();

    $('#month').html('');

    if(currYear == $('#year :selected').text()&& currMon==0){

        for(i=0; i<=currMon; i++)
          {
            $('#month').append($("<option />").val(option[i]).text(option[i]));
          }
    }else 
        {
        for(i=0; i<=11; i++)
      {
        $('#month').append($("<option />").val(option[i]).text(option[i]));
      }
        }
    

    });

    0 讨论(0)
提交回复
热议问题