html
<form action="" method="post"> <div><input type="submit" value="批量删除" /></div> <table border="1" cellpadding="0" cellspacing="0" style="width:100%;border-color:#ccc;text-align:center;"> <tr height='30'> <td width="2%"><input type="checkbox" value="''" name="qx" onclick="checkall(this)" /></td> <td width="2%">ID</td> <td width="5%">账户</td> </tr> <?php while($row1=mysql_fetch_assoc($res1)){ ?> <tr height='30'> <td><input type='checkbox' name='ids[]' value=<?php echo $row1['username']; ?> class='ck'/></td> <td><?php echo $row1['id']; ?> </td> <td><?php echo $row1['username']; ?></td> </tr> <?php } ?> </form>
利用js点击事件就可以轻松实现全选
<script> function checkall(qx) { //全选多选的选中状态 var ck = document.getElementsByClassName("ck"); //让下面所有的多选选中状态改变 if(qx.checked) { for(i = 0;i < ck.length ; i++) { ck[i].setAttribute("checked","checked"); //状态改变为选中 } }else { for(var i = 0;i < ck.length;i++) { ck[i].removeAttribute("checked"); //移除选中 } } } </script>
php
<?php header('Content-Type: text/html; charset=utf-8'); ob_start(); include('../config/db.php'); if(!empty($_POST)){ $arr = $_POST["ids"]; $str = implode("','",$arr);//拼接字符, $sql = "delete from consumer WHERE username in ('{$str}')"; $res = mysql_query($sql); if($res){ echo "<script>alert('删除成功')</script>"; header('refresh:0;url=./alldel.php'); die; }else{ echo "<script>alert('删除失败')</script>"; header('refresh:0;url=./alldel.php'); die; } } ?>