<form action="testlist.action" method="post">
<table id="myTable">
<tr>
<td>用户名<input type="text" name="list[0].username"/></td>
<td>密码<input type="text" name="list[0].password" /></td>
</tr>
</table>
<input type="button" value="添加" id="newBtn" /> <input type="button"
value="删除" id="delBtn" /> <input type="submit" value="提交"
id="submitBtn" />
</form>
<script src="../js/jquery-1.11.0.min.js" type="text/javascript"></script> <script type="text/javascript"> var i = 0; $(document) .ready( function() { $("#newBtn") .bind( "click", function() { i++; //alert(i); //alert($("input:checked")); $("#myTable") .append( "<tr><td>用户名<input type=\"text\" name=\"list["+i+"].username\"/></td></td><td>密码<input type=\"text\" name=\"list["+i+"].password\"/></td></tr>"); // alert(document.getElementById('sd').value); }); $("#delBtn").bind( "click", function() { //alert("删除"+i); //alert($("input:checked")); //$("input:checked").parent().parent().remove(); //$("#myTable").remove(); //alert($("#myTable input[name=aa]").val()); //$("table").remove(tag); $( "#myTable input[name='list[" + i + "].username']").parent() .parent().remove(); if (i >= 0) i--; }); }); </script>
后台接收参数
List<User> list = null;
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
if (list != null && !list.equals("")) {
for (Iterator<User> iterator = list.iterator(); iterator
.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getUsername() + " " + user.getPassword());
}
}
转载于:https://blog.51cto.com/shamrock/1378523