1个回答
展开全部
这是JSP页面:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script>
$(document).ready(function() {
var i = 2;
$("button").click(function() {
var div = $("div");
div.append("<br />");
div.append("用户" + i + ":<input type='text' name='username' />");
div.append("密码" + i + ":<input type='text' name='password' />");
i++;
});
});
</script>
<button>add an input</button>
<form action="register" method="post">
<div>
用户:<input type="text" name="username" />
密码:<input type="text" name="password" />
</div>
<input type="submit" />
</form>
这是Action:
public class RegisterAction extends ActionSupport {
private String[] username;
private String[] password;
public String execute() throws Exception {
for (int i = 0; i < username.length; i++) {
System.out.println(username[i] + ":" + password[i]);
}
return SUCCESS;
}
public String[] getUsername() {
return username;
}
public void setUsername(String[] username) {
this.username = username;
}
public String[] getPassword() {
return password;
}
public void setPassword(String[] password) {
this.password = password;
}
}
多个相同的name提交,会被合并成一个数组。
如果不用Struts2,也可以用
String[] username = request.getParameterValues("username");
String[] password = request.getParameterValues("password");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询