1、HTML
<tr>
<th width="40"><input type="checkbox" name="check" value="">
全選/全不選</th>
<th width="40">ID</th>
<th width="60" align="left">姓名</th>
<th width="60" align="left">性別</th>
<th width="60" align="left">年齡</th>
<th width="60" align="left">手機號</th>
<th width="60" align="left">體重</th>
<th width="60" align="left">身高</th>
<th width="60" align="left">狀態</th>
<th width="60">{:lang('ACTIONS')}</th>
</tr>
<foreach name="customer" item="vo">
<tr>
<td><input type="checkbox" class="J_check" data-yid="J_check_y" data-xid="J_check_x" name="id[]" value="{$vo.id}" ></td>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
</foreach>
2、js
<script>
$("input[name='check']").click(function(){
//判斷當前點擊的複選框處於什麼狀態$(this).is(":checked") 返回的是布爾類型
if($(this).is(":checked")){
$("input[name='id[]']").prop("checked",true);
}else{
$("input[name='id[]']").prop("checked",false);
}
});
function delAll(){
var ck = $("input:checked[name='id[]']");
if(ck.length == 0){
alert("請選擇,然後進行刪除");
return;
}
var f=confirm("確認刪除!!");
var userIds = new Array();
if(f){
$("input:checked").each(function(){
//將標籤的值放入數組中
userIds.push($(this).val());
//console.log($(this).val());//此處添加不能使用add add不是一個function
});
}
console.log(userIds);
$.ajax({
type:"post",
url:"{:url('Customer/deleteAll')}",
data:{"id":userIds},
dataType:"json",
//traditional:true,
success:function(data){
console.log(data);
if(data.code==1){
if(confirm("恭喜你刪除成功!點擊確認刷新頁面")){
// 刪除成功後發送請求,刷新頁面
$(location).attr("href",'/Admin/Customer/index');
//window.location.href="user/showUser";
} else if(confirm("刪除失敗!點擊確認刷新頁面")) {
$(location).attr("href","/Admin/Customer/index')");
}
}
}
});
}
</script>