选择选项从当前年份删除月份

时间:2017-02-05 06:17:02

标签: jquery

我有3个选择框,包含日期月份和年份。如果我选择当前年度,我希望从月份选择框中删除前几个月。如果我选择明年或其他年份,它应显示所有月份。我检查了很多方法,但没有正常工作。

如何删除当前年度过期的所有索引或月份,如果选择其他年份,则显示所有月份。

<select name="date">
<option value="0">Day</option>
<option value="2">1</option>
<option value="3">1</option>
<option value="4">1</option>
..
<option value="31">31</option>
</select>

<select name="month">
<option value="0">Month</option>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
..
<option value="dec">December</option>
</select>

<select name="year">
<option value="0">Year</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
...
<option value="2030">2030</option>
</select>

我现在的逻辑是这样的。

$(".year").on('change', function() {
   //get the current year
   if selected year is current year -> I got this right
   then remove expired months -> This is not working right 
   if selected year is others -> Selected month should remain there
   then show all the months 
});

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

<select name="month">
<option value="-1">Month</option>
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
..
<option value="11">December</option>
</select>

$(".year").on('change', function() {
   var currentDate = new Date();
   if($(this).val()== currentDate.getFullYear()){
      $("select [name=month] option:lt("+ currentDate.getMonth()  +")").prop("disabled", true);   
   }
   else{
      $("select [name=month] option").prop("disabled", false);    
   }

});

希望它有所帮助!!