如何使用JavaScript限制未来的月份 [英] how to restrict future month using javascript

查看:77
本文介绍了如何使用JavaScript限制未来的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个下拉菜单(月,年).我必须根据年份限制未来的月份.假设我选择"2014",它应该显示到一月为止的月份.我不知道如何解决此问题.请正确指导

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 how to sort out this issue. please guide properly

<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>    

推荐答案

尝试这种方式

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

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

快乐编码:)

这篇关于如何使用JavaScript限制未来的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆