需要根据条件隐藏选择选项

时间:2014-08-25 10:32:32

标签: jquery select

我选择了带月选项的元素。

<select class="" id="manufacturedMonth" name="manufacturedMonth">
<option value="-1">MM</option>
<option value="8">Aug</option>
<option value="7">Jul</option>
<option value="6">Jun</option>
<option value="5">May</option>
<option value="4">Apr</option>
<option value="3">Mar</option>
<option value="2">Feb</option>
<option value="1">Jan</option>
<option value="12">Dec</option>
<option value="11">Nov</option>
<option value="10">Oct</option>
<option value="9">Sep</option>
</select>

如果值大于当前月份,则需要隐藏所有选项

2 个答案:

答案 0 :(得分:5)

您可以使用:

var d = new Date(),
mnth = d.getMonth();//get current month     
$('#manufacturedMonth option').filter(function () {
    return parseInt(this.value,10) > mnth; //return all the option element that have value greater than curremt month
}).hide();

<强> Working Demo

答案 1 :(得分:0)

使用JavaScript隐藏这些选项。

// get current month
var d = new Date();
var m = d.getMonth();

// get all options
var options = document.getElementsByTagName('option');
// for every option
for ( var i  = 0 ; i < options.length ; i++ ) {
    // check if its month is bigger than the current month
    if ( options[i].getAttribute("value") > m ) {
        // don't display that option
        options[i].style.display = 'none';
    }
}