如何使用javascript限制未来月份

时间:2014-01-30 11:11:35

标签: javascript jquery struts2

我正在使用两个下拉菜单(月,年)。我要根据年限制未来月份。假如我选择'2014',它应该显示截至1月的月份。我没有任何线索如何解决这个问题。请正确指导

<div class="width2 margin-bottom margin-top">
  <div class="profile-text">
    <span class="red">*</span>&nbsp;Certified On
   </div>
   <div class="profile-right">
     <s:select list="#{'':'---Select Month---','Jan':'Jan','Feb':'Feb',
        'Mar':'Mar','Apr':'Apr','May':'May','Jun':'Jun','July':'July',
        'Aug':'Aug','Sep':'Sep','Oct':'Oct','Nov':'Nov','Dec':'Dec'}" 
        name="month" id="month" data-toggle="tooltip" 
        title="Please enter the month" cssStyle="height:32px; width:49%;"
        placeholder="Month" cssClass="profile-input "/> 


      <s:select list="yearList" name="year" id="year" data-toggle="tooltip" 
        title="Please Enter Year" headerKey="" headerValue="--Select Year--" 
        cssStyle="height:32px; width:49%;" placeholder="Year" 
        cssClass="profile-input">      
      </s:select>        

  </div>
</div>    

2 个答案:

答案 0 :(得分:0)

试试这种方式

 $('#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); 
    }   
 });

注意:月份选择的值应为011,代表jan到dec

快乐编码:)

答案 1 :(得分:0)

$('#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();

$( '#月')的html( '');

if(currYear == $('#year:selected')。text()&amp;&amp; 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]));
  }
    }

});