多选框的取值 前后台的数据交换用ajax 全步骤
给表单多一个属性 <th>选项</th>
<input type="checkbox" name="sProblem" value="${id1}" />
<input type="checkbox" name="sProblem" value="${id2}" />
name="sProblem" 要一样
把value使用id这样后面直接拿这个值传到后台
<input class="btn btn-primary" type="button" onclick="getTheCheckBoxValue()" value="一键删除"/>
这个是一个按钮键调动方法getTheCheckBoxValue()
function getTheCheckBoxValue(){
confirmx("确认要删除该批软件咨询吗",function(){
obj = document.getElementsByName("sProblem");
check_val = [];
for(k in obj){
if(obj[k].checked)
check_val.push(obj[k].value);
}
if(check_val.length>0){
$.post("${ctx}/website/consultation/deleteid",{id:JSON.stringify(check_val)},function(res){
//JSON.stringify()这个是用来传数组的方法 字符串可以不用写这个
location.href="${ctx}/website/consultation/?repage"
});
}else{
alert("没有选中");
}
});
}
后端
@RequestMapping(value = "deleteid")
@ResponseBody //这个一定要写进去 这个是让 return "repage";出去的是以json格式的数据 而不是跳转的路径地址
public String deleteid(YcSldSubscribe consultation,HttpServletRequest request,@RequestParam("id")List<String> id, RedirectAttributes redirectAttributes) {
for (String a : id) {
String ad = a.replaceAll("\\[|\\]", ""); //这个是删除[] 替换为没有
String d = ad.replace("\"", "");//这个是删除" " 替换为没有
YcSldSubscribe ycSldSubscribe = ycSldSubscribeService.get(d);
ycSldSubscribeService.delete(ycSldSubscribe);
}
return "repage";//传回到ajax里面的值
}