我现在需要实现一个注册的功能,里面需要添加生日的信息,所以我就用了3个下拉框来分别对应年,月,日,现在想实现他们之间的联动,数据初始化我是在FormBean中做的,但是在年和日onchange的时候我需要动态改变日的下拉框的数据,这个需要怎么来实现呢?
代码如下:
JSP页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="userdetail.title" /></title>
<link href="/css/common.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
function select(obj) {
var days = Array(28);
days[0] = "01";
days[1] = "02";
days[2] = "03";
days[3] = "04";
days[4] = "05";
days[5] = "06";
days[6] = "07";
days[7] = "08";
days[8] = "09";
days[9] = "10";
days[10] = "11";
days[11] = "12";
days[12] = "13";
days[13] = "14";
days[14] = "15";
days[15] = "16";
days[16] = "17";
days[17] = "18";
days[18] = "19";
days[19] = "20";
days[20] = "21";
days[21] = "22";
days[22] = "23";
days[23] = "24";
days[24] = "25";
days[25] = "26";
days[26] = "27";
days[27] = "28";
if (obj.length == 2) {
if (obj == "01" || obj == "03" || obj == "05" || obj == "07"
|| obj == "08" || obj == "10" || obj == "12") {
days.push("29");
days.push("30");
days.push("31");
} else {
days.push("29");
days.push("30");
}
}
}
</script>
</head>
<body>
<table class="table1" align="center">
<tr>
<td class="required"><bean:message key="userdetail.title" /></td>
</tr>
</table>
<html:form action="/userdetailinput" onreset="">
<table class="table" align="center">
<tr>
<td><bean:message key="userdetail.nickname" /></td>
<td><html:text styleClass="textbox" property="nickname"></html:text></td>
</tr>
<tr>
<td><bean:message key="userdetail.realname" /></td>
<td><html:text styleClass="textbox" property="realname"></html:text></td>
</tr>
<tr>
<td><bean:message key="userdetail.birthday" /></td>
<td><html:select styleClass="selectYear" property="birthdayYear"
onchange="select(this.options[this.selectedIndex].text);">
<html:optionsCollection property="years" label="year" value="yearId" />
</html:select><bean:message key="userdetail.year" /> <html:select
property="birthdayMonth" styleClass="selectMonth"
onchange="select(this.options[this.selectedIndex].text);">
<html:option value="01"></html:option>
<html:option value="02"></html:option>
<html:option value="03"></html:option>
<html:option value="04"></html:option>
<html:option value="05"></html:option>
<html:option value="06"></html:option>
<html:option value="07"></html:option>
<html:option value="08"></html:option>
<html:option value="09"></html:option>
<html:option value="10"></html:option>
<html:option value="11"></html:option>
<html:option value="12"></html:option>
</html:select><bean:message key="userdetail.month" /> <html:select
property="birthdayDay" styleClass="selectDay">
<html:optionsCollection property="days" label="day" value="dayId" />
</html:select><bean:message key="userdetail.day" /></td>
</tr>
<tr>
<td><bean:message key="userdetail.sex" /></td>
<td>男<html:radio property="usersex" value="Male" /> 女<html:radio
property="usersex" value="Female" /></td>
</tr>
<tr>
<td><bean:message key="userdetail.email" /></td>
<td><html:text styleClass="textbox" property="email"></html:text></td>
</tr>
<tr>
<td><bean:message key="userdetail.address" /></td>
<td><html:text styleClass="textbox" property="address"></html:text></td>
</tr>
<tr>
<td><bean:message key="userdetail.hobby" /></td>
<td><html:textarea styleClass="textarea" property="hobby"></html:textarea></td>
</tr>
<tr>
<td align="right"><html:submit value="提交">
</html:submit> <html:reset value="重置"></html:reset>
</tr>
</table>
</html:form>
</body>
</html>FormBean:
package userdetail.form;import java.util.Calendar;import org.apache.struts.validator.ValidatorForm;import userdetail.beans.DayBean;
import userdetail.beans.YearBean;public class UserDetailForm extends ValidatorForm { private static final long serialVersionUID = 1L; private YearBean[] years;
private DayBean[] days;
private String nickname;
private String realname;
private String birthdayYear = "2009";
private String birthdayMonth = "01";
private String birthdayDay = "01";
private String usersex="Male";
private String email;
private String address;
private String hobby; /*
 * 构造函数
 */
public UserDetailForm() {
years = getYear();
days = getDay(Integer.parseInt(birthdayYear), birthdayMonth); } /**
 * @return the nickname
 */
public String getNickname() {
return nickname;
} /**
 * @param nickname
 *            the nickname to set
 */
public void setNickname(String nickname) {
this.nickname = nickname;
} /**
 * @return the realname
 */
public String getRealname() {
return realname;
} /**
 * @param realname
 *            the realname to set
 */
public void setRealname(String realname) {
this.realname = realname;
} /**
 * @return the birthdayYear
 */
public String getBirthdayYear() {
return birthdayYear;
} /**
 * @param birthdayYear
 *            the birthdayYear to set
 */
public void setBirthdayYear(String birthdayYear) {
this.birthdayYear = birthdayYear;
} /**
 * @return the birthdayMonth
 */
public String getBirthdayMonth() {
return birthdayMonth;
} /**
 * @param birthdayMonth
 *            the birthdayMonth to set
 */
public void setBirthdayMonth(String birthdayMonth) {
this.birthdayMonth = birthdayMonth;
} /**
 * @return the birthdayDay
 */
public String getBirthdayDay() {
return birthdayDay;
} /**
 * @param birthdayDay
 *            the birthdayDay to set
 */
public void setBirthdayDay(String birthdayDay) {
this.birthdayDay = birthdayDay;
} /**
 * @return the usersex
 */
public String getUsersex() {
return usersex;
} /**
 * @param usersex
 *            the usersex to set
 */
public void setUsersex(String usersex) {
this.usersex = usersex;
} /**
 * @return the email
 */
public String getEmail() {
return email;
} /**
 * @param email
 *            the email to set
 */
public void setEmail(String email) {
this.email = email;
} /**
 * @return the address
 */
public String getAddress() {
return address;
} /**
 * @param address
 *            the address to set
 */
public void setAddress(String address) {
this.address = address;
} /**
 * @return the hobby
 */
public String getHobby() {
return hobby;
} /**
 * @param hobby
 *            the hobby to set
 */
public void setHobby(String hobby) {
this.hobby = hobby;
} /**
 * @return the years
 */
public YearBean[] getYears() {
return years;
} /**
 * @param years
 *            the years to set
 */
public void setYears(YearBean[] years) {
this.years = years;
} /**
 * @return the days
 */
public DayBean[] getDays() {
return days;
} /**
 * @param days
 *            the days to set
 */
public void setDays(DayBean[] days) {
this.days = days;
} /*
 * 验证是否是闰年
 */
private Boolean IsLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
return true;
}
return false;
} /*
 * 验证月份是否为大
 */
private Boolean IsBigMonth(String month) {
if (month.equals("1") || month.equals("3") || month.equals("5")
|| month.equals("7") || month.equals("8") || month.equals("10")
|| month.equals("12")) {
return true;
}
return false;
} /*
 * 获得年数
 */
private YearBean[] getYear() {
YearBean[] bean = new YearBean[100];
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);
for (int i = 0; i < 100; i++) {
bean[i] = new YearBean();
bean[i].setYear(Integer.toString(year - i));
}
return bean; } /*
 * 获得日数
 */
private DayBean[] getDay(int year, String month) {
DayBean[] bean = null;
if (month.equals("2") && IsLeapYear(year)) {
bean = new DayBean[29];
for (int j = 0; j < 29; j++) {
bean[j] = new DayBean();
bean[j].setDay(Integer.toString(j + 1));
}
} else if (IsBigMonth(month)) {
bean = new DayBean[31];
for (int j = 0; j < 31; j++) {
bean[j] = new DayBean();
bean[j].setDay(Integer.toString(j + 1));
}
} else {
bean = new DayBean[30];
for (int j = 0; j < 30; j++) {
bean[j] = new DayBean();
bean[j].setDay(Integer.toString(j + 1));
}
} return bean;
}
}
没有对Action进行操作。
我之前的想法是通过onchange事件来调用js方法,但是无法实现他,请高手指点!

解决方案 »

  1.   

    何必如此麻烦?直接加个 时间 js不就OK啦?想发个图给你看~太麻烦 - -
      

  2.   

    重写select表单的option对象。数据可以使用for循环得到。其他的无所谓。不过给你介绍个时间插件吧。全JS,很好用的说http://www.my97.net/
    这下你的烦恼全没有了
      

  3.   

    感觉太麻烦,用个JS脚本就OK了
      

  4.   

    3楼推荐的那个插件很好用,但是我还是想能够了解一下整个使用Struts来实现的过程,有哪位高手能够指点一下吗?
      

  5.   

    用网页日期选择控件吧,又简单又美观:http://js.bhcode.net/javascript/20090820/110.html