form表单提交数组数据
数组数据可以通过ajax提交给后台,但是如果要跳转页面的话ajax是无法跳转的,要到success做location.href的跳转,
先定义一个class是接受数组类型的
public class TicketCart { private int count; private String name; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
再定义一个对象来接受form表单提交的数组
public class TicketCarts { private List<TicketCart> ticketCarts; public List<TicketCart> getTicketCarts() { return ticketCarts; } public void setTicketCarts(List<TicketCart> ticketCarts) { this.ticketCarts = ticketCarts; } }
html代码 i代表数组下标
<form action="xxx" method="post"> <input name="ticketCarts[i].count" /> <input name="ticketCarts[i].name" /> <button type="submit">提交</button> <form>
后台接收:
public ModelAndView sellTicket(TicketCarts ticketCarts){
//把数据拿出来用list装着
List<TicketCart> ticketCart=ticketCarts.getTicketCarts();
//定义两个数组提取数据出来方便操作
int[] count=new int[ticketCart.size()];
String[] name=new String[ticketCart.size()];
//遍历list把数据赋给数组
for (int i=0;i<ticketCart.size();i++){
count[i]=ticketCart.get(i).getCount();
name[i]=ticketCart.get(i).getName();
}
}
这样就可以得到前端提交的两个数组。
其实可以直接用ajax更加方便
ajax代码
var count=new Array(); $.ajax({ type:"post", url:"xxx", data:{deleteNum:deleteNum}, success:function(data){ if(data.success){ deleteNum = []; } } });
参考文章:https://www.cnblogs.com/yunspider/p/6337872.html