0

请有人帮助我..我想通过代码点火器中的复选框选择删除多个项目..

我做了什么:我的视图页面看起来像这样

<script type="text/javascript" src="<?php echo base_url(); ?>/js/jquery_1_8_1_min.js"></script>
<script language="javascript">
$(function(){
    $("#selectall").click(function () {
        $('.case').attr('checked', this.checked);
    });
    $(".case").click(function(){
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    });
});
</script>

<?php
foreach($query as $row)
{
    ?>
    <tr><td><input name="checkbox[]" class="case" type="checkbox"  value="<?php print $row->reg_id; ?>"></td></tr>
    <?php 
}
?>

我的控制器代码:

function del($reg_id) //delete content
{
    $this->load->helper(array('form','url'));
    $this->load->model('registration');
    $query=$this->registration->delete($reg_id);
    $data['query']=$this->registration->cr_getall();
    $this->load->view('vw_register',$data);
}

这是我的模型:

public function del($reg_id)
{
    $this->load->database();
    $del=$this->db->delete('tbl_contctform',array('reg_id'=>$reg_id));
    if($del)
    {
        return $del;
    }
    else
    {
        return false;
    }
}

我知道我犯了一些错误。但我不知道如何清除。

可以使用 ajax 吗?请帮帮我。

4

2 回答 2

1

尝试这个,

$(".case").on('click',function(){
    var chk=$('.case:checked').length ? true : false;// if length is > 0 then return true
    $('#selectall').prop('checked',chk);// if chk true then check the selectAll checkbox         
});

看看小提琴

您需要创建一个并form submit调用function of controllerform submit

# Your Controller Function
function del($reg_id) //delete content
{
    $this->load->helper(array('form','url'));
    $this->load->model('registration');
    $checkbox=$this->input->post('checkbox');# Using Form POST method you can use whatever you want like GET
    $reg_id=$this->input->post('reg_id');# Using Form POST method
    $regId_array=array();
    foreach($checkbox as $key=>$checked)
    {
        if($checked!==FALSE)
            $regId_array[]=$reg_id[$key];
    }
    if(!empty($regId_array))# array of checked reg_id to delete
    {
        $query=$this->registration->del($checkbox,$id);# use del here not delete as your model says
    }
    else{
        die('No checkbox selected!!!');
    }

    $data['query']=$this->registration->cr_getall();
    $this->load->view('vw_register',$data);
}

# Your Model Function
public function del($reg_id)
{
    $this->load->database();
    $this->db->where_in('reg_id', $reg_id);# delete all reg_id in $reg_id array variable
    $this->db->delete('tbl_contctform');

    if($del)
    {
        return $del;
    }
    else
    {
        return false;
    }
}

你的视图应该是这样的,

<?php
foreach($query as $row)
{
    ?>
    <tr><td>
        <input name="checkbox[]" class="case" type="checkbox" />
        <input name="reg_id[]" type="hidden"  value="<?php print $row->reg_id; ?>">
    </td></tr>
    <?php 
}
?>
于 2013-09-05T07:10:14.300 回答
1

我改变了我的视图,控制器,模型这样..现在它工作正常

看法:

<form name="forms" action="delete_checkbox/" method="post">
    foreach($query as $row)
    {?>
    <tr>
        <td><input type="checkbox" name="forms[]" id="forms[]"
                   value="<?php print $row->reg_id; ?>"/>
    </tr?
<?php } ?>

控制器:

function delete_checkbox() {
    $dat = $this->input->post('forms');
    for ($i = 0; $i < sizeof($dat); $i++) {
        print_r($dat[$i]);
        $this->load->model('registration');
        $this->registration->delete_check($dat[$i]);
    }
    redirect('viewreg/showall', 'refresh');
}

有用!!

于 2013-09-06T04:03:07.617 回答