1、前台jsp界面:
pageEncoding="utf-8"%>
Insert title here$(function() {
//对每行的第一个复选框实现简单的单选
$("#tablelist input[name=goodscheck]").click(function(){
$("#tablelist input[name=goodscheck]").attr('checked',false);
$(this).get(0).checked=true;
});
//添加一行,就是将第一个tr复制,默认有一个tr
$("#addbtn").click(function(){
var tr =$("#tablelist tr").eq(0);
var temptr=$(tr).clone(true); //复制tr
$("input",temptr).val(''); //将tr下的值清空
$("#tablelist").append(temptr);
});
//删除一行,至少有两个tr才能删除
$("#delbtn").click(function(){
if($("#tablelist tr").length>1){
var checks = $("#tablelist input[name=goodscheck]:checked");
if(checks && checks.length==1){
$(checks).parent().parent().remove();
}
}
});
//提交表单
$("#submit").click(function(){
refresh('tablelist');
testForm.submit();
});
});
//后台接收的是数组,因此需要刷新数组的索引值。如果刷新提交的索引为userTests[0]和userTests[2](假如userTests[1]被删除),
//后台的List集合的大小为3,索引为1的对象被认为是null.
function refresh(jq){
$("#"+jq+" tr").each(function(j){
$(this).children('td').children('input').each(function(){
var curname = $(this).attr('name');
if(curname){
var curnamearr = curname.split(".") ;
if(curnamearr && curnamearr.length>1){
var val="";
for(var i=1;i
val+="."+curnamearr[i];
}
$(this).attr('name','userTests['+j+']'+val);
}
}
});
});
}
2、后台action代码:
private List userTests; //get set方法省略
public void test() {
if (userTests!=null) {
for(UserTest userTest: userTests){
logger.info(" ============ "+userTest +" operator: "+userTest.getOperator().getCode());
}
}else{
logger.info(" ============ userTest is null ");
}
}
3、javaBean对象
public class UserTest {
private String name;
private String code;
private String jtCode;
private TBLOperator operator;
...get set
}
public class TBLOperator implements Serializable {
private String code;
}
4、测试数据:
13:23:21,524 INFO [LoginAction] ============ UserTest [name=1, code=, jtCode=, operator=com.cjonline.foundation.evisa.entity.TBLOperator@1225261] operator: 11
13:23:21,524 INFO [LoginAction] ============ UserTest [name=2, code=, jtCode=, operator=com.cjonline.foundation.evisa.entity.TBLOperator@355104] operator: 22
13:23:21,524 INFO [LoginAction] ============ UserTest [name=4, code=, jtCode=, operator=com.cjonline.foundation.evisa.entity.TBLOperator@59e5ec] operator: 44444444
13:23:21,524 INFO [LoginAction] ============ UserTest [name=5, code=, jtCode=, operator=com.cjonline.foundation.evisa.entity.TBLOperator@5f77e] operator: 5555555555