控制器阵列中的Javascript无法正常工作

时间:2017-08-29 05:23:04

标签: javascript php jquery codeigniter

我想在控制器内部更改操作表单上的切换按钮,javascript无法正常工作,没有请求......

这个我的控制器在控制器ajax服务器端数据表中包含jquery ......

ajax_list.php

public function ajax_list()
    {
        $list = $this->model_masterdata_menu->get_datatables();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $i => $menu) {
            $no++;
            $row = array();
            $row[] = $menu->menu_id;
            $row[] = $menu->menu_nama;
            $row[] = $menu->menu_url;
            if($menu->statusmenu_nama == 'visible')
            {
              $row[] = '<form id="form_['.$i.']" method="post" action="'.site_url().'/menu/visible/'.$menu->menu_id.'">
                          <label class="switch">
                          <input type="hidden" value="0" name="cbcheck">
                          <input type="checkbox" name="visible['.$i.']" id="cb_['.$i.']" class="cb_['.$i.']" checked>
                          <span class="slider round"></span>
                          </label>
                        </form">
                        <script language=\"text/javascript\">
                        $(function(){
                         $("#cb_['.$i.']").on("change",function(){
                            $("#form_['.$i.']").submit();
                            });
                        });

                        </script>
                        ';
            }else{
              $row[] = '<form id="form2_['.$i.']" method="post" action="'.site_url().'/menu/unvisible/'.$menu->menu_id.'">
                          <label class="switch">
                          <input type="hidden" value="1" name="cbcheck">
                            <input type="checkbox" name="visible['.$i.']" id="cb2_['.$i.']" class="cb2_['.$i.']" value="1" >
                            <span class="slider round"></span>
                          </label>
                        </form>

                        <script language=\"text/javascript\">
                        $(function(){
                         $("#cb2_['.$i.']").on("change",function(){
                            $("#form2_['.$i.']").submit();
                            });
                        });
                        </script>
                        ';
            }

            $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit('."'".$menu->menu_id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
                  <a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="hapus('."'".$menu->menu_id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';

            $data[] = $row;
        }

        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->model_masterdata_menu->count_all(),
                        "recordsFiltered" => $this->model_masterdata_menu->count_filtered(),
                        "data" => $data,
                );
        //output to json format
        echo json_encode($output);

when I switch, no request

当我切换按钮时,没有请求。 谁能帮我?非常感谢你

2 个答案:

答案 0 :(得分:2)

你不能像这样通过Ajax加载JS。

您应该为同一个类添加要添加事件的所有元素:

<input type="checkbox" name="visible['.$i.']" id="cb_['.$i.']" class="some-class" checked>

然后你应该在主页面上添加该类的事件绑定,如下所示:

$(function() {
    // Registering an event this way, the event will also be added to all elements 
    // appended to the DOM at a later point, like through ajax.
    $('body').on('change', '.some-class', function () {
        // Submit the closest form
        $(this).closest('form').submit();
    });
});

答案 1 :(得分:1)

你应该这样试试:

  1. 将您的选择器id更改为class并从javascript删除controller代码

    $(function(){                             
      $("body").delegate(".cb2","change",function(){
          $(this).closest('form').submit
          //$("#form2_['.$i.']").submit();
      });
    });