【问题标题】:how to condition data table checkbox if check/uncheck如果选中/取消选中,如何调整数据表复选框
【发布时间】:2017-01-19 08:40:14
【问题描述】:

我不知道如何调整数据表中的复选框。我想判断数据表中的复选框是否选中,我将其插入我的数据库中,如果未选中,我将在数据库中删除它。我正在使用 codeigniter 框架。

这是我的控制器:

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);
    }

这是我的 javascript,当我单击复选框时,我设法提醒 user_id 的值,但我不知道如何设置它,无论是选中还是选中并再次取消选中。这里是:

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);

  });

这是我的看法:

 <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>

【问题讨论】:

    标签: javascript mysql codeigniter checkbox datatables


    【解决方案1】:

    <!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>

    在 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");
        }
    
    
    });
    

    【讨论】:

    • 您好,先生,这就是我所做的:检查工作正常,但取消检查 user_id 的值不起作用。这是代码: $('#dataTables-docs tbody').on('click', 'input[type="checkbox"]', function(e){ if($(this).is(":checked ")) { var user_id = $(this).val(); alert(user_id+"checkbox is check"); } else { alert(user_id+"Checkbox Is Not Checked"); } });
    • 尝试 // .on('change',而不是 .on('click',
    • 我需要为 onchange 创建另一个函数吗?你能编辑你的答案吗?谢谢
    【解决方案2】:
    $('#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
            }
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多