jquery 全选 全不选 批量删除

全选全不选按钮然后点击批量删除

< th >全选 < input type=“checkbox” id=“theadInp” />< /th >

<input name=“ids” class=“ids” type=“checkbox” value="{ {$v.id}}" / >

$(function() {

    //实现全选反选
    $("#theadInp").on('click', function() {
    
        $("tbody input:checkbox").prop("checked", $(this).prop('checked'));
        
    })
    
    $("tbody input:checkbox").on('click', function() {
    
        //当选中的长度等于checkbox的长度的时候,就让控制全选反选的checkbox设置为选中,否则就为未选中
        
        if($("tbody input:checkbox").length === $("tbody input:checked").length) {
        
            $("#theadInp").prop("checked", true);
            
        } else {
        
            $("#theadInp").prop("checked", false);
        }
    })
})


function datadel(){

    $ids = $("input[name='ids']:checked");
    
    var id=[];
    
    $("input[name='ids']:checked").each(function(i){
    
        id[i] = $(this).val();

    });
    //判断数组是否为空。空的话禁止点击
    
    if(id.length == 0){
    
        return;
        
    }
    
    layer.confirm('确认要删除吗?',function(index){

        $.ajax({
        
            type: 'POST',
            
            url: "{
   
   {:MyUrl('admin/goods/delall')}}",
            
            data:{id:id},
            
            dataType: 'json',
            
            success: function(data){

                $ids.each(function(i){
                
                    $(this).parents("tr").remove();
                    
                });
                
                layer.msg('已删除!',{icon:1,time:500});
                
            },
            
            error:function(data) {
            
                console.log(data.msg);
            },
        });
    });
}

猜你喜欢

转载自blog.csdn.net/yanruixiang123/article/details/108538126