您的位置:首页 > 编程语言 > Java开发

Struts + js日期选择源码

2005-06-02 14:05 381 查看

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" onchange="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" onchange="toDate()" styleId="month">
<html:options collection="monthCollection" property="value" labelProperty="label"/>
</html:select>月
<html:select property="sendDay" onchange="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.onload=toDate; <!—首次装载页面的时候调用toDate生成day下拉选项-->
//-->
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: