Ajax Dropdown将填补省和市

时间:2017-06-16 13:47:50

标签: javascript php ajax cakephp cakephp-3.x

我使用Ajax为我的下拉菜单选择省和城市。

我选择省后,我希望城市能够居住。

我已经有了我的代码,但遗憾的是它不起作用。

这是我的代码

的javascript

<script type="text/javascript">
function provChange(){
  var province = $("#province_id").val();
  $.ajax({
    url: "<?php //echo $this->Html->url('/front/ajax_city_id'); ?>/"+province+"/<?php //echo $this->request->data('Car.city_id'); ?>",cache: false,
    success: function(msg){$("#city_id").html(msg); },
    "statusCode": {
      403: function() {
        window.location.href="<?php //echo $this->Html->url(array('controller'=>'front','action'=>'index')); ?>"
      },
      500: function() {
        alert('Error Server Side occured');
      }
    }
  });
}
</script>

view.ctp

<div class="emBeli form large-9 medium-8 columns content">
<?php echo $this->Form->input('provinsi',array('type'=>'select','empty'=>'Provinsi','options'=>$provinsis,'id'=>'province_id','class'=>'form-control form-control-custom','label'=>false,'onChange'=>'provChange()')); ?>

<?php echo $this->Form->input('kota',array('type'=>'select','empty'=>'Kota','options'=>$kota,'class'=>'form-control form-control-custom','label'=>false)); ?>
</div>

ajax_city_id.ctp

<?php
if ($key==null) echo "<option value=\"\">Kota</option>";
    foreach($types as $i):
            if($i['Kota']['id']==$key) echo "<option value=\"".$i['Kota']['id']."\" selected>".$i['Kota']['name']."</option>";
            else echo "<option value=\"".$i['Kota']['id']."\">".$i['Kota']['name']."</option>";
    endforeach;
?>

Controller.php这样

public function ajax_city_id($id = null, $key = null, $cur = null)
{
    $this->layout = 'ajax';
    $this->set('types', $this->Kota->find('all', array('conditions' => array('display' => 1, 'Kota.province_id =' => $id), 'order' => array('Kota.name' => 'asc'))));
    $this->set('key', $key);
}

我得到的是ReferenceError:找不到变量:$ 这意味着它无法在我的javascript代码中找到变量$(“#province_id”)。

请有人帮忙......

0 个答案:

没有答案