效果:javascript
JQuery代碼:java
<script type="text/javascript">
$(document).ready(function () {
// 全選
$("#selectAll").click(function () {
$("input[name='RKEY']").prop("checked", this.checked);
});
// 單選
var subChk = $("input[name='RKEY']")
subChk.click(function () {
$("#selectAll").prop("checked", subChk.length == subChk.filter(":checked").length ? true : false);
});
/* 批量刪除 */
$("#deleteBtn").click(function () {
// 判斷是否至少選擇一項
var checkedNum = $("input[name='RKEY']:checked").length;
if (checkedNum == 0) {
alert("至少選擇一項!");
return;
}
// 批量選擇
if (confirm("肯定要刪除所選項目?")) {
var checkedList = new Array();
$("input[name='RKEY']:checked").each(function () {
checkedList.push($(this).val());
});
$.ajax({
type: "POST",
url: "../Logs/DeleteMore",
data: { 'delitems': checkedList.toString() },
dataType: "text",
success: function (result) {
alert(result);
$("[name ='RKEY']:checkbox").attr("checked", false);
window.location.reload();
}
});
}
});
});
</script>ajax
HTML代碼:sql
<table class="table table-bordered table-striped table-hover">
<tbody>
<tr align="center">
<td nowrap="nowrap" style="width: 120px; margin-left: 50px;">
<input type="checkbox" id="selectAll" name="selectAll" style="margin-left: 50px;" />
<input id="deleteBtn" type="button" role="button" class="btn btn-info btn-small" value="刪除" />
</td>
<td nowrap="nowrap"><strong>序號</strong></td>
<td nowrap="nowrap"><strong>日誌類型</strong></td>
<td nowrap="nowrap"><strong>操做用戶</strong></td>
<td nowrap="nowrap"><strong>登陸IP</strong></td>
<td nowrap="nowrap"><strong>操做內容</strong></td>
<td nowrap="nowrap"><strong>操做時間</strong></td>
<td nowrap="nowrap"><strong>備註</strong></td>
</tr>
@foreach (var item in Model)
{
i++;
<tr align="center">
<td nowrap="nowrap"><input type="checkbox" id="RKEY" name="RKEY" value="@item.RKEY" /></td>
<td nowrap="nowrap">
@item.Counts
</td>
<td nowrap="nowrap">
@Html.DisplayFor(modelItem => item.LogType)
</td>
<td nowrap="nowrap">
@Html.DisplayFor(modelItem => item.UserAccount)
</td>
<td nowrap="nowrap">
@Html.DisplayFor(modelItem => item.LoginIP)
</td>
<td nowrap="nowrap">
@Html.DisplayFor(modelItem => item.Operation)
</td>
<td nowrap="nowrap">
@Html.DisplayFor(modelItem => item.CreateDate)
</td>
<td nowrap="nowrap">
@Html.DisplayFor(modelItem => item.Remark)
</td>
</tr>
}
</tbody>
</table>ui
後臺代碼:this
/// <summary>
/// 批量刪除日誌數據
/// </summary>
/// <returns></returns>
public ActionResult DeleteMore()
{
ArrayList arr = new ArrayList();
string rkeyStr = "";
StringBuilder sb = new StringBuilder();
if (Request["delitems"] != null && Request["delitems"].ToString() != "")
{
rkeyStr = Request["delitems"].ToString();
string[] rkeyArr = rkeyStr.Split(',');
int count = 0;
for (int i = 0; i < rkeyArr.Length; i++)
{
string sqlStr = "delete from Logs where RKEY=" + Convert.ToInt32(rkeyArr[i]) + "";
count = SqlHelper.ExecuteSql(sqlStr);
}
if (count > 0)
{
log.DeleteLogs("批量刪除日誌數據");
string str = "批量刪除成功!";
return Content(str);
}
}
else
{
rkeyStr = "";
string str = "批量刪除失敗!";
return Content(str);
}
return null;
}url