简体   繁体   English

动态依赖于Codeigniter的下拉菜单

[英]Dynamic dependent Dropdown on Codeigniter

I am working on a project that uses a dynamic dependent dropdown on codeigniter. 我正在使用对Codeigniter使用动态依赖下拉列表的项目。 I am sorry I am quite new in coding. 对不起,我在编码方面还很陌生。 I have a table that gets the id of a service that is working but getting the other table that is dependent on that service is not working. 我有一个表,该表获取正在运行的服务的ID,但获取依赖于该服务的另一个表却无法运行。 I used the console tool in google chrome and shows that it gets the option value of the other table but it shows a blank select input in my browser. 我在谷歌浏览器中使用了控制台工具,并显示它获取了另一个表的选项值,但在浏览器中显示了空白的选择输入。

shows the option value in my dbase 显示我的数据库中的选项值

here is my code: controller: 这是我的代码:控制器:

public function get_procedures()
{
    $servID = $this->input->post('servID');
    $result = $this->Laboratory_Model->get_all_products1($servID);
    if(count($result)>0)
    {
        $pro_select_box = '';
        $pro_select_box .= '<option value="">Select s</option>';
        foreach ($result as $rows) {



         $pro_select_box .='<option value="'.$rows->itemNum.', '.$rows->prodName.' ">'.$rows->prodName.'</option>';
        }
        echo $pro_select_box;
    }
}

js: js:

       $('#servID').on('change', function(){
            var servID = $(this).val();
            if(servID == '')
            {
                $('#servPRO').attr('disabled', true);
            }
            else
            {
                $('#servPRO').attr('disabled', false);

                $.ajax({
                    url:"<?php echo base_url() ?>get_procedures",
                    type: "POST",
                    data: {'servID' : servID},
                    dataType: 'html',

                    success: function(data){
                       $('#servPRO').html(servID);
                    },
                    error: function(){
                        alert('Error occur...!!');
                    }
                });
            }
       }); 

Laboratory model: 实验室型号:

        public function get_all_products1($servID){
    $query = $this->db->get_where('laboratory', array('servID' => $servID));
    if($query->num_rows() > 0){
        return $query->result();
    } else {
        return NULL;
    }
}

views: 意见:

    <select name ="servID" id="servID" class="form-control" data-live-search="true">
                      <option value = 'null'>-Select Category-</option>
                          <?php foreach($service as $rows): ?>
                              <option value = '<?php echo $rows->servID?>'><?php echo $rows->service_cat?></option>
                          <?php endforeach ?>
                  </select>
    </select> <select class="form-control"  name="servPRO" id="servPRO">
    </select>

You have mistyped return variable with request parameter on your ajax success method, the supplied variable should be data instead of servID : 您在ajax 成功方法上输入了带有请求参数的返回变量,但输入的变量应该是data而不是servID

$.ajax({
    url:"<?php echo base_url() ?>get_procedures",
    type: "POST",
    data: {'servID' : servID},
    dataType: 'html',

    success: function(data){
       $('#servPRO').html(data); // change servID to data
    },
    error: function(){
        alert('Error occur...!!');
    }
});

Edit 编辑

Change the default ServID select 更改默认的ServID选择

<option value='null'>-Select Category-</option>

to

<option value=''>-Select Category-</option>

because 'null' is not equal to '' . 因为'null'不等于''

And set initial state of the dropdown : 并设置下拉菜单的初始状态:

$('#servPRO').attr('disabled', $('#servID').val() == '' ? true : false ); // set initial state

$('#servID').on('change', function(){
    var servID = $(this).val();
    if(servID == '')
    {
        $('#servPRO').attr('disabled', true);
    }
    else
    {
        $('#servPRO').attr('disabled', false);

        $.ajax({
            url:"<?php echo base_url() ?>get_procedures",
            type: "POST",
            data: {'servID' : servID},
            dataType: 'html',

            success: function(data){
               $('#servPRO').html(data); // changed servID to data
            },
            error: function(){
                alert('Error occur...!!');
            }
        });
    }
});

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

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