简体   繁体   English

如果选中/取消选中,如何调整数据表复选框

[英]how to condition data table checkbox if check/uncheck

i dont know how to condition the checkbox inside the datatable. 我不知道如何调节数据表中的复选框。 I want to condition if the checkbox in data table is check, i will insert it in my database, and if it is uncheck , i will delete it in database. 我想确定数据表中的复选框是否为选中状态,我将其插入数据库中,如果未选中,则将其删除为数据库中的复选框。 I am using a codeigniter framework. 我正在使用一个codeigniter框架。

Here is my controller: 这是我的控制器:

public function getalldocs() {
        $listdocs = $this->Admin_model->getdoctors();
        $data = array();
        foreach ($listdocs as $docs) {
            $row = array();  
            $row[] = $docs->user_fname;
            $row[] = $docs->user_mname;
            $row[] = $docs->user_lname;
            $row[] = '<input name="user_id[]" value="'.$docs->user_id.'" type="checkbox">';

            $data[] = $row;
        }
        $output = array(   
            "data" => $data,
        );
        echo json_encode($output);
    }

here is my javascript, i manage to alert the value of user_id when i click the checkbox but i dont know how to condition it, wether it was check or checked and uncheck again. 这是我的Javascript,当我单击复选框时,我设法提醒user_id的值,但是我不知道如何调节它,无论它是选中还是选中并再次取消选中。 Here it is: 这里是:

function show_docs() {
    $("#dataTables-docs").dataTable().fnDestroy();

    table =  $('#dataTables-docs').DataTable({ 
      "ajax": {
              "url": "<?php echo site_url('admin_controls/getalldocs')?>",
              "type": "POST",
          },
          responsive: true,
          className: 'select-checkbox',
          'bInfo': false,
          'paging': false
      });
}

$('#dataTables-docs tbody').on('click', 'input[type="checkbox"]', function(e){

  var user_id = $(this).val();
  alert(user_id);

  });

Here is my view: 这是我的看法:

 <table id="dataTables-docs"  class="table table-striped table-bordered table-hover dataTable dtr-inline" role="grid" style="width: 100%;" width="100%" aria-describedby="dataTables-material">
                    <thead>
                        <tr> 
                            <th>First Name</th>
                            <th>Middle Name</th>
                            <th>Last Name</th>                                               
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
    </tbody>
</table>

 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <body> <table id="dataTables-docs" class="table table-striped table-bordered table-hover dataTable dtr-inline" role="grid" style="width: 100%;" width="100%" aria-describedby="dataTables-material"> <thead> <tr> <th>First Name</th> <th>Middle Name</th> <th>Last Name</th> <th><input type="checkbox" value="sdsd">Check</th> </tr> <tr> <th>First Name</th> <th>Middle Name</th> <th>Last Name</th> <th><input type="checkbox" value="sdsd">Check</th> </tr> <tr> <th>First Name</th> <th>Middle Name</th> <th>Last Name</th> <th><input type="checkbox" value="sdsd">Check</th> </tr> </thead> <tbody> </tbody> </table> <script> $(document).on('change', 'input[type="checkbox"]', function(e){ if($(this).is(":checked")) { alert("Checkbox Is Checked"); } else { alert("Checkbox Is Not Checked"); } }); </script> </body> </html> 

Check with is(":checked") in jquery 在jQuery中检查is(“:checked”)

$(document).on('change', 'input[type="checkbox"]', function(e){

      if($(this).is(":checked"))
    {
      alert("Checkbox Is Checked");
    }
    else
    {
    alert("Checkbox Is Not Checked");
    }


});
$('#dataTables-docs tbody').on('click', 'input[type="checkbox"]', function(e){
    for(i = 0; i < $("input[type="checkbox"]").length; i++){
        if($(this).porp("checked")){
            //delete function
        }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM