下面是一个年月日用JS动态改变天数的例子代码:
MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function yearChanged(year,type){
var month='';
if(type=='start')
month = document.getElementById("startMonth");
else
month = document.getElementById("endMonth");
var MMvalue = month.value;
var n = MonHead[MMvalue - 1];
if (MMvalue ==2 && IsPinYear(year.value))
n++;
writeDay(n,type)
}
function monthChanged(month,type){
var startyear ='';
if(type=='start')
startyear=document.getElementById("startYear");
else
startyear=document.getElementById("endYear");
var YYYYvalue = startyear.value;
var monthNum=month.value;
var n = MonHead[monthNum - 1];
if (monthNum ==2 && IsPinYear(YYYYvalue))
n++;
writeDay(n,type);
}
function writeDay(n,type)
{
var startday='';
if(type=='start')
startday= document.getElementById("startDay");
else
startday= document.getElementById("endDay");
var e = startday;
optionsClear(e);
for (var i=2; i<(n+1); i++){
if(i<10){
var temp='0'+i;
e.options.add(new Option(temp,temp));
}else{
var temp=i;
e.options.add(new Option(temp,temp));
}
}
}
function IsPinYear(year)
{
return(0 == year%4 && (year%100 !=0 || year%400 == 0));
}
function optionsClear(e)
{
e.options.length = 1;
}
select 下拉列表框默认有一个<option selected="selected"></option> 选择,如果是直接往上面动态添加option时,这
默认项还是存在,可能会导致前两项重复,可以用下面的方法将其取消!
function removeFirstItem(s){
if(s.options[0].text=='01'){
options[0]=null;
}
options[0].selected='selected';
}
|