通过点击事件利用from表单追加div
<script>
var i=0;定义变量 每次追加一个i就增加1
function add(sid){
$("form").append(
"<div>"+
"<table>"+
"<tr>"+
"<td><input type='text' name='pl["+i+"].pf'></td>"+
"<td><input type='text' name='pl["+i+"].comment'></td>"+
"<td><input type='hidden' name='pl["+i+"].sid' value='${sid}'></td>"+
"</tr>"+
"</table>"+
"</div>"
);
i=i+1;
}
</script>
<body>
<input type="button" value="我要点评" onclick="add(${sid})">
<table >
<tr>
<td>序号</td>
<td>评分</td>
<td>评论内容</td>
</tr>
<c:forEach items="${zlist }" var="z">
<tr>
<td>${z.pid }</td>
<td>${z.pf }</td>
<td>${z.comment }</td>
</tr>
</c:forEach>
</table>
</body>
<form>
<!--<div>
<table>
<td><input type="text" name="pf"></td>
<td><input type="text" name="comment"></td>
<td><input type="hidden" name="sid" value="${sid}"></td>
</table>
</div>-->
</form>
批量添加
定义两个类 一个是要准备添加各个属性的类 一个是封装了集合的类
public class Pl {
private int pid;
private double pf;
private String comment;
private int sid;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPf() {
return pf;
}
public void setPf(double pf) {
this.pf = pf;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
public Pl( int pid,double pf, String comment, int sid) {
super();
this.pid = pid;
this.pf = pf;
this.comment = comment;
this.sid = sid;
//this.sname = sname;
}
public Pl() {
super();
}
@Override
public String toString() {
return "Pl [pid=" + pid +" pf=" + pf + ", comment=" + comment + ", sid=" + sid + "]";
}
封装了集合的类
public class ListPlForm {
private List<Pl> plf; //集合的泛型的要添加的属性的类
public List<Pl> getPl() {
return plf;
}
public void setPl(List<Pl> pl) {
this.plf = pl;
}
public ListPlForm(List<Pl> pl) {
super();
this.plf = pl;
}
public ListPlForm() {
super();
}
@Override
public String toString() {
return "ListPlForm [pl=" + plf + "]";
}
进入controller
@RequestMapping("/tj")
@ResponseBody
public boolean tj(ListPlForm plf) { //传进来的参数的封装了集合的类
List<Pl> pl = plf.getPl();//得到集合
for (Pl pl2 : pl) {//遍历集合 得到对象 循环添加
cs.tj(pl2);
}
return true;
}