struts2~列表~checkbox~删除多条记录

转载自: http://hi.baidu.com/%BA%FC%C0%EA%C7%D1/blog/item/2855deccbc2ccd1900e928e3.html

 

jsp里~~

<script type = "text/javascript">

function testDelete(){
flag = false;
var de = document.getElementsByName("delid");
for(i = 0; i < de.length; i++){
if(de[i].checked == true){
flag = true;
break;
}
}
if(flag == false){
alert("至少选择一个待删除记录");
return false;
}
if(confirm("确定删除?")) {
testForm.action = "../test/delete.action";
testForm.submit();

}


}
</script>

<body>
<s:fielderror></s:fielderror>
<s:form theme = "simple" method = "post" name = "testForm">
<table >
<input name=testDel" type="button" value="删除" onClick="testDelete()">
<s:iterator value="testList" status="st">
<tr>
<td>
<s:hidden name = "testId"/>
<input name = "delid" type = "checkbox" value = "${testId}" id = "delid[#st.index]" >
</td>
<td><s:property value="testName"></s:property></td>

</tr>
</s:iterator>
</table>
</body>

action里~~

private long[] delid = new long[]{};

public long[] getDelid() {
return delid;
}
public void setDelid(long[] delid) {
this.delid = delid;
}

public String deleteRecord() throws Exception {//删除记录
String idSting = "";
for(int i=0;i<this.delid.length;i++){
idSting=idSting+this.delid[i]+",";
}
idSting=idSting.substring(0,idSting.length()-1);//待删除记录若干id的字符串,逗号分隔
testService.delete(idSting);
return SUCCESS;
}


自己写滴~~功能上实现了~~没进行服务器端校验~~先记录下~以后再改进~~

另~
获得form中checkbox个数可以用
testForm.delid.length ~~
但要注意~
当只有一个checkbox时~
testForm.delid.length 滴值为undefined~
就是说~当只有一条记录时无法删除~~
所以还是乖乖用
document.getElementsByName("delid") ~然后用.length获得个数~~

再另~~
百度空间文章滴排版真杯具~贴进来滴代码滴缩进被无视了~

你可能感兴趣的:(JavaScript,html,jsp,百度,Blog)