展示頁面代碼如下
<form action="depts.do?flag=delete" name="myform" method="post">
<input type="checkbox" value="1" name="cb" />1
<input type="checkbox" value="2" name="cb" />2
<input type="checkbox" value="3" name="cb" />3 <input type="button" value="提交1" onclick="getvalues()"/> <input type="submit" value="提交2" />
</form>
一、利用js+jQuery獲取復選框內容
<script type="text/javascript">
function getvalues(){
//定義一個空數組
var arr=[];
$("input[name='cb']:checked").each(function(i){
//把所有被選中的復選框的值存入數組
arr[i]=$(this).val();
})
console.log(arr);
}
</script>
二、在控制層獲取復選框的內容
@RequestMapping(value = "depts.do",params = "flag=delete")
//String視圖解析器
public String delete(String[] cb,ModelMap mp) {
//String[] cb cb:對應復選框的name值
System.out.println(cb.length);
deptsMapper.deleteBatch(cb);
return "depts";
}