html form表单全选 批量删除

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>

猜你喜欢

转载自blog.csdn.net/weixin_36851500/article/details/103265424
今日推荐