ajax带checkbox选择值到后台
function togglefun(checkbox){//全选 $('input[name=cids]').prop('checked', $(checkbox).prop('checked')); }
//执行操作将值传到后台 function sysnycdata(){ if($("input[name=cids]:checked").length<1){ alert("请至少选择一条数据"); return false } $.ajax({ cache: true, type: "POST", url:"后台地址", data:$("#cidssformsubmit").serialize(), async: false, error: function(request) { alert("Connection error"); }, success: function(data) { data=eval("("+data+")"); } }); }
<form action="?" method="post" id="cidssformsubmit" > <table class="adv_query_list"> <tbody> <tr class="search_title_show"> <td align="center"><input type="checkbox" name="idschk" onclick="togglefun(this)" /></td> </tr> <tr align="center" class="tdbg"> <td><input type="checkbox" name="cids" value="${it[0]}" /></td> </tr> </tbody> </table> </form>
@RequestMapping(value = "execute") @ResponseBody public String execute(Long[] cids,HttpServletRequest request, Model model) throws Exception { int errorCount=0; try { if(null!=cids&&cids.length>0){ for (int i = 0; i < cids.length; i++) { System.out.println(cids[i]); } } } catch (Exception e) { } return "{'errorCount':'"+errorCount+"'}"; }