js 全选/全不选/反选 php for表单实现批量删除

<form class="form form-horizontal" action="{:U('huati/sdel')}" method="post">   
    <table class="table table-border table-bordered table-bg">
        <thead>
        <tr class="text-c">
            <th width="20">选择</th>
            <th width="20">ID</th>
            <th width="100">图片</th>            
            <th width="180">话题发表人</th>
            <th width="100">所属话题圈</th>
            <th width="180">话题内容</th>            
            <th width="80">操作</th>
        </tr> 

        </thead>
        <tbody id="news_option">
        <!-- 遍历 -->
        <volist name="productlist" id="v" empty="暂时没有数据">
            <tr class="text-c">
                <td><input type="checkbox" class="ck" id="cex" name="item[]" value="{$v.id}"></td>
                <td>{$v.id}</td>
                <!--<td style="padding:3px 0;"><img src="__DATA__/{$v.photo_x}" width="80px" height="80px"/></td>-->
                <td style="padding:3px 0;"><img src="__DATA__/{$v.photo1}" width="180px" height="80px"/></td>                
                <td>{$v.name}</td>
                <if condition="$v.cid eq 1"><td><label style="color:green;">{$v.cname}</label></td></if>
                <if condition="$v.cid eq 2"><td><label style="color:green;">{$v.cname}</label></td></if>
                <if condition="$v.cid eq 3"><td><label style="color:green;">{$v.cname}</label></td></if>
                <td>{$v.content}</td>
                <td>
                    <a οnclick="del_id_urls({$v.id})">删除</a> <!--|
                    <a href="{:U('Huati/pinglun')}?id={$v.id}&page={$page}&name={$name}&shop_id={$shop_id}&tuijian={$tuijian}">评论</a>-->
                </td>
            </tr>
        </volist>
        <!-- 遍历 -->
        </tbody>
        <tr>
            <td colspan="10" class="td_2">
                {$page_index}
            </td>
        </tr>
        <tr>
            <td colspan="10" class="td_2">
               <!--<input type="submit" style="width:70px;height:35px;background-color:#e61c15"value="批量删除"/>-->
               <button type="submit" class="btn btn-danger">批量删除</button>
            </td>
        </tr>        
    </table>
    </form>
    <tr>
        <td colspan="6">
            <button id="btn1">全选</button>
            <button id="btn2">全不选</button>
            <button id="btn3">反选</button>            
            
        </td>
    </tr>

js代码实现全选/全不选/反选

<script type="text/javascript">
        var a=document.getElementById('cex');
        var b=document.getElementsByTagName('input');
        //全选
        document.getElementById('btn1').οnclick=function(){
            for (var i = 0; i < b.length; i++) {
                b[i].checked=true;
            }
        };
        //全不选
        document.getElementById('btn2').οnclick=function(){
            for (var i = 0; i < b.length; i++) {
                b[i].checked=false;
            }
        }; 

    //如果有未选中的,取消全选
        /*function select(){
            var count=0;
            for (var i = 0; i < b.length; i++) {
                if (b[i].checked) {
                    count++;
                    //console.log(count);
                }
            }
            if (count==b.length-1) {
                a.checked=true;
            }else{
                a.checked=false;
            }
        }
        //如果有未选中的,取消全选
        for (var j = 0; j < b.length; j++) {
            b[j].οnclick=function(){
                select();
            };
        }*/

        //反选
        document.getElementById('btn3').οnclick=function(){
            for (var i = 0; i < b.length; i++) {
             var value=b[i];
             if (value.checked) {
                 value.checked=false;
             }else{
                 value.checked=true;
             }
          }
         select();
        };
</script>

php代码实现批量删除

public function sdel(){
        $id = $_POST["item"];       
        foreach($id as $key=>$val){      
            $result = M('huati')->where('id='.$val)->delete();//循环删除话题

            $res = M('pinglun')->where('h_id='.$val)->delete();//删除话题的同时删除话题下的评论和回复
        }
        echo "<script> {window.alert('话题删除成功');location.href='index.html'} </script>";       
    }

也可以用这个方法

$str = implode("','",$id);//拼接字符,
$result = M('huati')->where("$val in ($str)")->delete();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值