複選框checkbox實現批量刪除

一、jsp頁面

<script type="text/javascript" src="<%=path%>/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
//全選,反選
$('#chooseAll').click(function() {
var list = $('[name=ids]').length;
if ($('#chooseAll').attr("checked") == true) {
for ( var i = 0; i < list; i++) {
$('[name=ids]').attr("checked", "true");
}
} else if ($('#chooseAll').attr("checked") == false) {
$('[name=ids]').click();
}
});


//一、檢查是否已選,jquery實現
$('[name=batchdelete]').click(function() {
var l=$("input[type=checkbox]:checked").size(); //size是選中的個數
if(l==0){
alert("未勾選要刪除的對象");
}else{
alert("提交表單: "+l);
$('#form1').submit();
}

});


/*  二、 //檢查是否已選js實現

var list = $('[name=ids]').length;
//alert("changdu "+list);
var a=0;

for( var n = 0; n < list; n++){
if($('[name=ids]')[n].checked==false){
a++;
alert("a: "+a);
alert("changdu "+list);
if(a===list){
alert("未勾選要刪除的對象");
return false;
}  
}
if($('[name=ids]')[n].checked==true){
alert("勾選對象");
//parent.document.location.href="<%=path%>/counselorDeleteActionDo?id="+id;
return true;
}
}*/

});
</script>

<body>

<a href="mass_selectionpackage/mass_dataList.action">獲得數據</a>
<form name="form1" id="form1" action="mass_selectionpackage/mass_batchDelete.action" method="post" >
<table>
<tr>
<th width="10%" colspan="1"></th>
<th width="15%">
序號
</th>
<th width="15%">
Id
</th>
<th width="30%">
Name
</th>
<th width="30%">
Time
</th>
</tr>
<s:iterator value="#request.imgList" id="imgs" status="rowstatus">
<tr>
 
<td >
<input type="checkbox"name="ids" id="ids" value="<s:property value="#imgs.id" />" />
</td>

<td><s:property value="#rowstatus.index+1"/></td>
<td>
<s:property value="#imgs.id" />
</td>
<td>
<s:property value="#imgs.imgname" />
</td>
<td>
<s:property value="#imgs.imgoperatingtime" />
</td>
<td>
<a
href="mass_selectionpackage/mass_deleteById.action?id=<s:property value="#imgs.id" />&content=<s:property value="#imgs.imgname" />&title=${title}">delete</a>
</td>
</tr>
</s:iterator>
<tr>
<td>
<input type="checkbox" name="chooseAll" id="chooseAll" />
</td>
<td>
ChooseAllOrUnchoose
</td>
<td>
<input name="batchdelete" type="button"  value="batchdelete">
</td>
</tr>
</table>
</form>

</body>


二、java類,action


package com.wm.mass.action;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wm.bean.ActivityImg;


public class MassSelectionAction extends ActionSupport {
/**
* @author
*/
private static final long serialVersionUID = 8422741115018483046L;

private int id;
private int temcount=5;

private Object[] ids;//接收復選框中的id數組
private ActivityImg activityImg=new ActivityImg();
private List<ActivityImg> imgList1=new ArrayList<ActivityImg>();
private List<ActivityImg> imgList;
private boolean flag;

{
for (int i = 1; i < temcount; i++) {
activityImg.setId(i);
activityImg.setImgname("名稱——"+i);
activityImg.setImgoperatingtime("時間——:"+i);
imgList1.add(activityImg);
}
}
public String dataList() {
imgList=this.imgList1;
return SUCCESS;
}

public String deleteById() throws UnsupportedEncodingException {

System.out.println("刪除:"+getContent()+", id:"+getId());
temcount--;
return this.dataList();
}

/*
* 批量刪除
*/
public String batchDelete() throws UnsupportedEncodingException {

for(int i=0;i<ids.length;i++){
 // employeeService.delete(ids[i]);
  System.out.println("刪除:"+getContent()+", id:"+ids[i]);
 }
return this.dataList();
}
 
public List<ActivityImg> getImgList() {
return imgList;
}
public void setImgList(List<ActivityImg> imgList) {
this.imgList = imgList;
}
 
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public Object[] getIds() {
return ids;
}
public void setIds(Object[] ids) {
this.ids = ids;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章