<select name="select1" id="select1" onchange="onSelect2()">
<option value="">--请选择--</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2009">2008</option>
<option value="2009">2007</option>
<option value="2009">2006</option>
</select>
年
<select name="select2" id="select2">
<option value="" >--请选择--</option></select>
月function onSelect2(){
var year = document.getElementById("select1").value;
var myDate = new Date();
var mYear = myDate.getFullYear();
var html ;
var thisMonth =myDate.getMonth()+1;
var lastMonth = thisMonth-1;
if(year == mYear){
for(var i=0;i<lastMonth;i++){
select2.options[i] = new Option(lastMonth-i,lastMonth-i);
}
}
if(year != mYear){
for(var i = 0;i < 12 ; i++){
select2.options[i] = new Option(12-i,12-i);
}
}
}
问题是这样的,如果我一开始2010的时候,很正常。然后我选select1中的其他值的时候,select2的值很正常,然后我在select1中选2010就出现问题了,出现的问题是在select2中出现了1——(lastMonth-2) 的值重复出现。
select2.length=0
再填充
var year = document.getElementById("select1").value;
var myDate = new Date();
var mYear = myDate.getFullYear();
var html ;
var thisMonth =myDate.getMonth()+1;
var lastMonth = thisMonth-1;
if(year == mYear){
select2.length=0
for(var i=0;i<lastMonth;i++){
select2.options[i] = new Option(lastMonth-i,lastMonth-i);
}
}
if(year != mYear){
for(var i = 0;i < 12 ; i++){
select2.options[i] = new Option(12-i,12-i);
}
}
}
year = Number(year);转换下类型