2017-02-05 30 views
0

我有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 
}); 
+0

顯示我們的HTML代碼隊友 – rahulsm

回答

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);  
    } 

}); 

希望它可以幫助!