我试图在一个下拉列表中隐藏和显示标签,最初我成功地让它在除Internet Explorer之外的所有地方工作。然后我发现将需要隐藏的选项包含在<optgroup>标签中可以解决IE的问题。但是我现在遇到了一个问题,因为我编写的代码也删除了它们所包含的下拉列表。我做错了什么?以下是目前的代码:
function GetMonthsForSelectedYear() {
var selectedYear = $("#DropDownListYear option:selected").text(),
currentYear = (new Date()).getFullYear(),
currentMonth = (new Date()).getMonth() + 1;
$("#DropDownListMonth option").each(function() {
if (selectedYear == currentYear) {
var option = $(this).index();
if (option < currentMonth) {
$(this).wrap('<span>').hide();
}
} else {
$(this).unwrap("<span>").show();
}
});
}
$("#DropDownListYear").change(function() {
GetMonthsForSelectedYear();
});
以下是JSFiddle链接:
谢谢。