简体   繁体   English

动态依赖下拉CodeIgniter Ajax不起作用

[英]Dynamic Dependent Dropdown CodeIgniter ajax not working

I'm trying to populate the second dropdown base on the first dropdown using ajax in codeigniter but it seems not it's not populating nor displaying the alert error in my ajax, it does nothing. 我正在尝试在codeigniter中使用ajax填充基于第一个下拉列表的第二个下拉列表,但似乎不是没有填充也不在我的ajax中显示警报错误,它什么也不做。 Is there anything I miss or typo? 有什么我想念或错字的吗? thank you very much. 非常感谢你。

My View 我的观点

<select name="carbrand" class="form-control" required="true" id="carbrand">
<option value="" selected disabled>--- Select Car Brand ---</option>
<li role="separator" class="divider"></li>
                        <?php
                        foreach($groups as $row){
                        echo '<option value="'.$row->brand.'" >'.$row->brand.'</option>';
                                }
                                ?>

   </select>
   </div>
   //SECOND DROPDOWN
   <div class="form-group">
   <select name="carmodel" class="form-control" id="carmodel" disabled="">
   <option value="" selected disabled>--- Select Car Model ---</option>
   </select>

   //SCRIPT
   <script>
   $(document).ready(function(){
   $('#carbrand').on('change', function(){
    var carbrand = $(this).val();
    if(carbrand == '')
    {
      $('#carmodel').prop('disabled', true);
    }
    else
    {
      $('#carmodel').prop('disabled', false);
      $.ajax({
        url:"<?php echo base_url() ?>/quote_aia/get_models",
        type:"POST",
        data:{'carbrand' : carbrand },
        dataType: 'json',

        success: function(data)
        {
          $('#carmodel').html(data);
        },
        error:function()
        {
          alert('Error');
        }
      });
    }
   });
 });

My Controller 我的控制器

     public function get_models()
     {  
    $this->load->model('quote'); 
    $carbrand = $this->input->post('carbrand');
    $brand = $this->quote->get_model_query($carbrand);
    if(count($brand)>0)
    {
        $cb_select = '';
        $cb_select .='<option value="">Select Model </option>';
        foreach ($brand as $brando) {
            $cb_select .='<option value="'.$brando->model.'"> '.$brando->model.'</option>';
        }
        echo json_encode($cb_select);
    }
}

My Model 我的模特

 public function get_model_query($carbrand)
{
    $query = $this->db->get_where('tbl_fmv', array('brand' => $carbrand));
    return $query->result();
}

Please help me thankyou very much 请非常帮助我谢谢

立即修复,我只是重新编码,不知道我错过了什么,但已修复

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

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