jsp 复选框多选删除(设计 jsp js ajax)
1、前台(JSP)
· (1)、调用
<input style="width:80px;margin-left:10px;margin-top:4px;" class="scbtn"id="btnClick" type="button" onclick="checkeds()" value='删除' >
<table class="tablelist">
<thead>
<tr>
<th>
<input type="checkbox" id="reverseSelect" onclick="OnAllCheck()" />
</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>系别</th>
<th>班级</th>
<th>电话</th>
<th>邮箱</th>
<th colspan="2">操作<i class="sort"><img src="${pageContext.request.contextPath}/resources/images/px.gif" /></i></th>
</tr>
</thead>
<tbody>
<!-- 以下数据应从数据表遍历而得 -->
<c:forEach items="${page.result}" var="item">
<tr id="1">
<td width="80">
<input type="checkbox" name="check_all_box" id="check_all_box" value="${item.stu_id}"/>
</td>
<td width="80">${item.stu_num}</td>
<td width="80">${item.stu_name}</td>
<td width="10">${item.gender}</td>
<td width="80">${item.department}</td>
<td width="80">${item.classT}</td>
<td width="80">${item.phone}</td>
<td width="80">${item.eamil}</td>
<td width="20">
<a href='${ctx}/update/${item.stu_id}'>
<img src="${resourcePath}/images/edit.gif" border="0">
</a>
</td>
<td width="20">
待用
</td>
</tr>
</c:forEach>
</tbody>
</table>
2、前台(js)
/*------多选删除 ----------*/
function checkeds()
{
var ids='';
$("input[name='check_all_box']:checkbox").each(function(){
if (true == $(this).attr("checked")){
ids += $(this).attr('value')+',';
}
});
if(ids!=''){
if(confirm('确定删除吗 ?')){
$.ajax({
data:"ids="+ids,
type:"POST",
dataType:'json',
url:"${ctx}/deleteStu",
success:function(data){
if(data.bol==true){
window.location.href="${ctx}/queryStuInf";
}else{
window.location.href="${ctx}/queryStuInf";
}
}
});
}
}else{
alert("您没有选择一条数据,请选择!!!");
}
}
3、java代码
/**
* 多选删除
* @param ids
* @param data
* @param session
* @param request
* @return
*/
@RequestMapping(value="/deleteStu")
@ResponseBody
public Map<String, Object> deleteStu(@RequestParam(value="ids",defaultValue="") String ids ,
Student data,HttpSession session,HttpServletRequest request) {
System.out.println("ids:"+ids);
Map<String, Object> idsMap=new HashMap<String, Object>();
StudentService service=new StudentService();
boolean bol=false;
String []ids1=ids.split(",");
for (int i = 0; i < ids1.length; i++) {
System.out.println("id:" + ids1[i]);
if (!ids1[i].isEmpty()) {
int id = Integer.parseInt(ids1[i]);// 将String 转 int
data.setStu_id(id);
bol = service.deleteStuById(data, session);
idsMap.put("bol", bol);
} else {
System.out.println("系统错误");
idsMap.put("msg", "系统错误");
}
}
return idsMap;
}
ps: 1、配置好springMVC配置文件
2、导入相应的包配置json格式解析
3、jsp引入Jquery文件(ajax需要)