您的位置:首页 > Web前端 > JavaScript

jsp页面添加一个集合数组到action(用序列化提交)

2015-12-21 11:56 801 查看
页面的js

//点击a标签增加删除
var i=0;
$("#a").on("click",function(){
var $newtr = $("<tr  id='model'>"
+"<td><input type='text' name='contactsList["+i+"].contactName' value=''></td>"
+"<td><input type='text' name='contactsList["+i+"].contactTel' value=''></td>"
+"<td><input type='text' name='contactsList["+i+"].contactFax' value=''></td>"
+"<td><input type='text' name='contactsList["+i+"].contactEmail' value=''></td>"
+"<td><input type='text' name='contactsList["+i+"].contactRole' value=''></td>"
+"<td ><a href='javascript:void(0);' class='del'>删除</a></td>"
+"</tr>");
i++;
$newtr.find(".del").click(function(){
$(this).parents("tr").remove();
});
$("#fourdiv").append($newtr);
});


页面jsp

<div id="a" style="color:blue;cursor:pointer"><h4>添加一个联系人</h4></div>
<div id="thirddiv">
<table id="fourdiv">
<tr>
<th>姓名</th>
<th>电话</th>
<th>传真</th>
<th>邮箱</th>
<th>职务</th>
<th>操作</th>
</tr>
</table>
</div>


action中接收直接用集合接收就好(遍历成对象来添加到数据库)

private List<Contacts> contactsList = new ArrayList<Contacts>();//页面接收联系人的数组
public List<Contacts> getContactsList() {
return contactsList;
}

public void setContactsList(List<Contacts> contactsList) {
this.contactsList = contactsList;
}
//添加多条联系人
for(Contacts contacts:contactsList){
contacts.setCustomId(customs.getId());
this.customsService.addContacts(contacts);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: