下拉阿贾克斯州和城市无法运作

时间:2017-04-13 13:16:06

标签: javascript php jquery ajax codeigniter

我有一份包含州和城市下拉的注册表。但是,在选择州后,它无法获得城市列表。

在控制台中,以下内容对我来说就像回答:

  

无法加载资源:服务器响应状态为404(未找到)。

Tablas de region y comuna

    |----------------------------|
    |        tbl_state           |
    |----------------------------|
    |        id_state            |
    |        name_state          |  
    |----------------------------|

    |----------------------------|
    |        tbl_city            |
    |----------------------------|
    |        id_city             |
    |        name_city           |
    |        state_id            |
    |----------------------------|

这是我的代码。

控制器:

public function index()
    {
        //State
        $data['result_state'] = $this->state_model->getState();

        $this->load->view('interprete_registro_view',$data);

    }

    public function city()
    {
        $id = $this->input->get('id_state');

        $this->state_model->getCity($id);
    }

型号:

public function getState() 
    {
        $this->db->select('*');
        $this->db->from('tbl_state');
        $query = $this->db->get();
        $query_result = $query->result();
        return $query_result;
    }

    public function getCity($id)
    {
        $query = $this->db->where('state_id', $id)->get('tbl_city');

        $cad = "";

        foreach ($query->result_array() as $row) {
            $cad .= "<option value='{$row['id_city']}'>{$row['name_city']}</option>";
        }

        echo $cad;
    }

查看:

<script type="text/javascript">
  var path = '<?php echo base_url()?>index.php/';

   $(document).ready(function() {
     carga();

     $('#state').change(carga);

   });

   function carga () {
     var cd = $('#state').val();

     $.get(path + 'registre/city', {'id' : cd}, function(resp) {
       $('#city').empty().html(resp);
     });
   }

</script>
        <div class="col-2">
          <label for="state">State <span> * </span></label>
        </div>

        <div class="col-3">
          <select class="form-control" name="state" id="state">      
            <option value="" selected>- state -</option>
              <?php foreach($result_state as $row):?>
                <option value="<?php echo $row->id_state;?>"><?php echo $row->name_state;?></option>
              <?php endforeach; ?>
           </select>
        </div>

        <div class="col-2">
          <label for="city">City <span> * </span></label>
        </div>

        <div class="col-3">
          <select class="form-control" name="city" id="city">
            <option>- city -</option>"
          </select>
        </div>

欢迎任何想法,解决方案或维度。

1 个答案:

答案 0 :(得分:0)

在视图中,JavaScript您正在传递名为id的get参数,但在控制器中,您正试图获取id_state

function carga () {
     var cd = $('#state').val();

     $.get(path + 'registre/city', {'id_state' : cd}, function(resp) {
       $('#city').empty().html(resp);
     });
   }