2016-10-17 17 views
1

私はゾーン、州、市でcasecadingしています。コントローラとビューに次のコードを追加しました。しかし、私のJavascriptの機能onchangeビュー形式で動作していない、コントローラ用javascript関数がcakephpで動作していません

コードを助けてください:

<?php   
    class StatesController extends AppController { 

     public function beforeFilter() { 
      parent::beforeFilter(); 
      $zones = $this->{$this->modelClass}->Zone->find("list", array('conditions' => array('is_active' => 1))); 
      $this->set(compact('zones')); 

      //allow actions 
      $this->Auth->allow(array('get_states')); 
     } 

     public function add() { 
      parent::add(); 
      $this->loadModel('Zone'); 
      //Renders the common form for add and edit actions 
      $this->render('form'); 
     } 

     * Get States. list According to zone_id by ajax call 
     */ 
     function get_states() { 
      $this->autoRender = false; 
      $zone_id = 0; 
      if (!empty($_POST['zone'])) { 
       $zone_id = $_POST['zone']; 
      } 
      $states = $this->State->find('list', array(
       'conditions' => array(
        'zone_id' => $zone_id, 
        'is_active' => ACTIVE 
       ), 
       'order' => array('name' => 'asc') 
       )); 

      $states = array_flip($states); 
      echo json_encode($states); 
     } 

    }` 
code for view 
<code> 
<aside class="body_rt"> 
    <!--Page head start--> 
    <div class="page_head"> 
     <h1> <?php echo ucwords($model); ?> Manager</h1> 
     <div class="back"> 
      <?php echo $this->Html->link($this->Html->image('images/back.png'), array(
          'action' => 'index'), array('escape' => false, 'title' => "Back")); 
      ?> 
     </div> 
    </div> 
    <!--Page head end--> 
    <!--Message head start--> 
    <?php if (($this->Session->check('Message.failure'))) { ?> 
    <section class="bolg_warp custom_message"> 
     <div class="sub_head">Message</div> 
     <div class="flashMessage failure-flash"> 
       <?php echo $this->Session->flash('failure'); ?> 
     </div> 
    </section> 
    <?php } ?> 
    <!--Message head end--> 
    <!--Blog warp start--> 
    <section class="bolg_warp"> 
     <?php echo $this->Form->create($model, array('type' => 'file', 
       'inputDefaults' => array('label' => false, 'div' => false))); ?> 
     <div class="three_column"> 

      <ul> 
       <li> 
        <?php echo $this->Form->hidden('id', array('class' => 'text_field')); ?> 
       </li> 
       <li> 
        <h2>Outlet Code:</h2> 
        <h3> 
      <?php echo $this->Form->input('outlet_code', array('class' => 'text_field')); ?> 
        </h3> 
       </li> 
       <li> 
        <h2>Name: <span class ="required">*</span></h2> 
        <h3> 
      <?php echo $this->Form->input('name', array(
           'class' => 'text_field')); ?> 
        </h3> 
       </li> 

       <li> 
        <h2>Zone :</h2> 
        <h3> 
         <?php echo $this->Form->input('zone_id', array(
           'options' => $zones, 
           'label' => false, 
           'div' => false, 
           'value' => '', 
           'class' => 'text_field short_field', 
           'empty' => 'All' 
          )); 
         ?> 
        </h3> 
       </li> 
       <li> 
        <h2>State :</h2> 
        <h3> 
         <?php echo $this->Form->input('state_id', array(
           'options' => $states, 
           'label' => false, 
           'div' => false, 
           'value' => '', 
           'class' => 'text_field short_field', 
           'empty' => 'All' 
          )); 
         ?> 
        </h3> 
       </li> 



       <li> 
        <h2>Status: </h2> 
        <h3> 
          <?php echo $this->Form->input("is_active", array("type" => "checkbox", 
          "class" => "form-control checkbox")) ?> 
        </h3> 
       </li> 
      </ul> 
     </div> 
     <div class="centerbutton"> 
      <ul> 
       <li> 
        <?php echo $this->Form->submit('images/save.png' , array('class'=>'ajax-submit')); ?> 
       </li> 
       <li> 
        <?php 
      echo $this->Html->link($this->Html->image('images/cancle.png'), array('controller' => $controller, 'action' => 'index'), 
         array('escape' => false)); 
        ?> 
       </li> 
      </ul> 
     </div> 
     <?php echo $this->Form->end(); ?> 
    </section> 
    <!--Blog warp end--> 
</aside> 

<!--cascding --> 
<script> 
    $(function(){ 
    var domain = "http://" + document.domain; 

     $("select#CityZoneId").on('change', function() { 
      var zone_id = $(this).val(); 
      if (zone_id != '') 
      { 
       var url = domain + "/states/get_states"; 

       $.post(url, {zone: zone_id}, function (data) { 

        var obj = $.parseJSON(data); 
        $("#CityStateId option").remove(); 
        $("#CityStateId").append("<option value=''>All</option>") 
        $.each(obj, function (i, value) { 
         $("#CityStateId").append("<option value='" + value + "'>" + i + "</option>") 
        }); 
       }); 
      } 
     }); 
    }); 
</script> 

私が間違っ

答えて

0

をやっているところを教えてくださいあなたは呼び出さないように見えますあなたの入力と同じIDで、jQueryをこれに変更してみてください:

$("#zone_id").on('change', function() { //your stuff }

関連する問題