【问题标题】:Dropdown Ajax state and city is not working下拉阿贾克斯州和城市不起作用
【发布时间】:2017-04-13 23:10:52
【问题描述】:

我有一个包含州和城市下拉列表的注册表。但是选择州后无法获取城市列表。

在控制台中,以下对我来说似乎是答案:

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

地区和社区表

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

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

【问题讨论】:

  • http://www.yourUrl.com/index.php/registre/city 在浏览器中是单独工作还是返回 404?如果可行,您的脚本设置中的path 是否正确?
  • 在'config.php'中分配给$config['base_url'] 的是什么?您是否使用.htaccess 重写您的网址?

标签: javascript php jquery ajax codeigniter


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2018-07-04
    相关资源
    最近更新 更多