Struts + js日期选择源码
Web页面里,日期选择是一个很头疼的问题,闰年,平年,大月小月,是一个三级的联动下拉选单,这里我用的是JavaScript的解决方案
下面是代码
<%
Vector yearCollection = new Vector();
for (int i = new GregorianCalendar().get(GregorianCalendar.YEAR); i >= 1900; --i) {
yearCollection.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), String.valueOf(i)));
}
java.util.Vector monthCollection = new java.util.Vector();
for (int i = 1; i <= 12; ++i) {
monthCollection.add(new org.apache.struts.util.LabelValueBean(String.valueOf(i), String.valueOf(i)));
}
pageContext.setAttribute("yearCollection", yearCollection);
pageContext.setAttribute("monthCollection", monthCollection);
%>
<html:select property="sendYear" οnchange="toDate()" styleId="year">年
<!—styleId Identifier to be assigned to this HTML element (renders an "id" attribute). -->
<html:options collection="yearCollection" property="value" labelProperty="label"/>
</html:select>
<html:select property="sendMonth" οnchange="toDate()" styleId="month">
<html:options collection="monthCollection" property="value" labelProperty="label"/>
</html:select>月
<html:select property="sendDay" οnchange="toDay()" styleId="day">日
</html:select>
<script language="JavaScript">
<!--
function toDate(){
with(document.all){
vYear=parseInt(year.options[year.selectedIndex].text)
vMonth=parseInt(month.options[month.selectedIndex].text)
day.length=0;
for(i = 1; i <= (new Date(vYear,vMonth,0)).getDate(); i++) {
day.options[day.length++].value=i;
day.options[i-1].text=i;
}
}
toDay();
}
window.οnlοad=toDate; <!—首次装载页面的时候调用toDate生成day下拉选项-->
//-->
</script>