日期:2014-05-17  浏览次数:20506 次

新手求指教!jQuery实现全选与批量删除??
这是数据库循环取出来的ID
<TD><input type="checkbox" name="check" value="<?php echo $row['adm_id'] ?>"/></TD>

这是几个button
<TR><TD align="right" height="25">
<input type="button" id="batch_del" value="批量删除" />
<input type="button" id="checkedAll" value="全选" />
  <input type="button" id="checkedRev" value="反转" /></TD></TR>

这是全选与反转代码
$(function(){

  $("#checkedAll").click(function(){
$("[name=check]:checkbox").attr('checked',true);
});

$("#checkedRev").click(function(){
$("[name=check]:checkbox").each(function(){
this.checked=!this.checked;
});
});

});

但怎样实现批量删除??求高手指教!! 先谢啦!!


------解决方案--------------------
<input type="checkbox" name="check[]" value="<?php echo $row['adm_id'] ?>"/></TD>
------解决方案--------------------
PHP code

<?php
if($_SERVER['REQUEST_METHOD'] === "POST"){
    foreach($_POST as $chk => $post){
         $pos = strpos($chk,"check");
        if(!$pos === false)
            $ids[] = $post;
    }
    if(count($ids) > 1)
        $id = implode(",",$ids);
    else
        $id = implode($ids);
    $sql = "DELETE FROM [table] WHERE id IN (".$id.")";
    $result = mysql_query($sql,$conn);
    if(!$result)
              die("SQL: {$sql} <br>Error: " . mysql_error() );
    if(mysql_affected_rows() > 0)
        echo "<script>alert('已批量删除');</script>";
    else
        echo "<script>alert('批量删除失败\nError:".mysql_error()."');</script>";
}
?>

<?php foreach($rows as $row){ ?>
<TD><input type="checkbox" name="check[]" value="<?php echo $row['adm_id'] ?>"/></TD>
<?php } ?>