实现目标: 两个日期,有下拉框: 复制代码 代码如下: //导入jquery地址 <BR>//startYear发生变化 <BR>function changeYear(str,isstart) <BR>{ <BR>var pre = "start"; <BR>if(isstart == false) <BR>{ <BR>pre = "end"; <BR>} <BR>var startMonth = $(pre + "Month").value; <BR>if(startMonth == "") <BR>{ <BR>var e = $(pre + "Month"); <BR>optionClear(e); <BR>return; <BR>} <BR>var n = MonHead[startMonth - 1]; <BR>if(startMonth == 2 && IsPinYear($(pre + "Year").value)) <BR>{ <BR>n++; <BR>} <BR>writeDay(n,pre); <BR>} <BR>function changeMonth(str,isstart) <BR>{ <BR>var pre = "start"; <BR>if(isstart == false) <BR>{ <BR>pre = "end"; <BR>} <BR>var year = $(pre + "Year").value; <BR>if(year == "") <BR>{ <BR>var e = $(pre + "Day"); <BR>optionClear(e); <BR>return; <BR>} <BR>var n = MonHead[str - 1]; <BR>if(str == 2 && IsPinYear($(pre + "Year"))) <BR>{ <BR>n++; <BR>} <BR>writeDay(n,pre); <BR>} <BR>function dateStart() <BR>{ <BR>var defaultStartY = "2011"; <BR>var defaultStartM = "1"; <BR>var defaultStartD = "14"; <BR>var defaultEndY = "2011"; <BR>var defaultEndM = "2"; <BR>var defaultEndD = "23"; <BR>MonHead = [31,28,31,30,31,30,31,31,30,31,30,31]; <BR>var prestr = new Array("start","end"); <BR>for(var j=0; j<2; j++) <BR>{ <BR>var pre = prestr[j]; <BR>//start 年 <BR>var y = new Date().getFullYear(); <BR>if(pre == "start") <BR>{ <BR>//start初始选中前第10天 <BR>var i_index = 0; <BR>for(var i=(y-10); i<=y; i++) <BR>{ <BR>$(pre+"Year").options.add(new Option(""+i+"",i)); <BR>if(i == defaultStartY) <BR>{ <BR>$(pre+"Year").options[i_index].selected = true; <BR>} <BR>i_index++; <BR>} <BR>} <BR>else <BR>{ <BR>var i_index =0; <BR>for(var i=(y-5); i<=y+5; i++) <BR>{ <BR>$(pre+"Year").options.add(new Option(""+i+"",i)); <BR>if(i == defaultEndY) <BR>{ <BR>$(pre+"Year").options[i_index].selected = true; <BR>} <BR>i_index++; <BR>} <BR>} <BR>//start月 <BR>defaultM = (pre == "start" ? defaultStartM : defaultEndM) <BR>for(var i = 1; i < 13; i++) <BR>{ <BR>$(pre + "Month").options.add(new Option(""+i+"",i)); <BR>if(i == defaultM) <BR>{ <BR>$(pre + "Month").options[i-1].selected = true; <BR>} <BR>} <BR>//start日 <BR>var n = MonHead[$(pre + "Month").value]; <BR>if(new Date().getMonth == 1 && IsPinYear($(pre + "Year").value)) <BR>{ <BR>n++; <BR>} <BR>defaultD = (pre == "start" ? defaultStartD : defaultEndD) <BR>writeDay(n,pre); <BR>$(pre + "Day").options[defaultD-1].selected = true; <BR>} <BR>} <BR>function writeDay(n,pre) <BR>{ <BR>var e = $(pre + "Day"); <BR>optionClear(e); <BR>for (var i=1; i<(n+1); i++) <BR>{ <BR>e.options.add(new Option(""+i+"",i)); <BR>} <BR>} <BR>function IsPinYear(year) <BR>{ <BR>return (0 == year%4 && (year%100 != 0 || year % 4 == 0)); <BR>} <BR>function optionClear(e) <BR>{ <BR>for(var i=e.options.length; i>=0; i--) <BR>{ <BR>e.remove(i); <BR>} <BR>} <BR> 从 年 月 日 到 年 月 日 参考了一些代码,希望对需要的朋友有所帮助。