2016-08-03 42 views
0

我在表單中有兩個用於選擇國家和州的從屬選擇框。javascript代碼從列表框中獲取多個選中的值php

我有選擇的國家, 多選擇複選框下拉列表,我想取選定的國家

的狀態,但我不明白如何通過多從我的javascript功能檢查的ID。

下面是我的HTML選擇列表框代碼

<?php 
             echo $this->Form->input('UserLogDetail.service_area_category_id', array(
              'id' => 'shipping_type', 
              'required' => false, 
              'multiple' =>'multiple', 
              'type' => 'select',           
              'class' => 'form-control drop-arrow', 
              'label' => false, 
              'options' => $serviceCategory, 
              'empty' => '--Select--' 
             )); 
             ?> 

<?php 
             echo $this->Form->input('UserLogDetail.skills', array(
              'class' => 'form-control', 
              'required' => false, 
              'id' => 'skill', 
              'label' => false, 
              'options' => '$states', 
              'empty' => '--Select--' 
             )); 
             ?> 

javascript函數

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#shipping_type").on('change', function() { 
      var id = $(this).val();   
      if (id) { 
       var dataString = 'id=' + id; 
       $.ajax({ 
        type: "POST", 
        url: '<?php echo Router::url(array("controller" => "Profiles", "action" => "loadSkills"),true); ?>', 
        data: dataString, 
        dataType: 'JSON', 
        cache: false,     
        success: function(html) {      
         $("#skill").html(""); 
         $.each(html, function(key, value) { 
          $('<option>').val('').text('select'); 
          $('<option>').val(key).text(value).appendTo($("#skill")); 
         }); 
         $('#skill').selectpicker('refresh'); 
        } 
       }); 
      } 
     }); 
}); 
</script> 

controlller功能

public function loadSkills() { 
     $this->loadModel('Skill'); 
     $states = array(); 
     if (isset($this->request['data']['id'])) { 
     $states = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
     'Skill.service_area_category_id IN' => explode(",",$this->request['data']['id'])))); 
     }   
     echo json_encode($states); 
     exit(); 
    } 

回答

0

嘗試撥打以下方式AJAX

的javascript:

$("#shipping_type").change(function() { 

    $.ajax({ 
       type: "POST", 
       url: '<?php echo Router::url(array("controller" => "Profiles", "action" => "loadSkills"),true); ?>', 
       //passing data to php 
       data: { id:$(this).val() }, 
       dataType: 'JSON', 
       cache: false,     
       success: function(html) {      
        $("#skill").html(""); 
        $.each(html, function(key, value) { 
         $('<option>').val('').text('select'); 
         $('<option>').val(key).text(value).appendTo($("#skill")); 
        }); 
        $('#skill').selectpicker('refresh'); 
       } 
      }); 
}); 

PHP

public function loadSkills() { 
    $this->loadModel('Skill'); 
    $states = array(); 
     //Getting post id 
    if (isset($this->request['id'])) { 
    $states = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
    'Skill.service_area_category_id IN' => explode(",",$this->request['data']['id'])))); 
    }   
    echo json_encode($states); 
    exit(); 
} 
+0

但複選框選擇事件loadskills功能不會從我的AJAX調用....我是使用多個複選框選擇標籤選擇多個國家... plz請參閱下面的URL參考.http://users.skavt.net/blaz/MultiSelect/demo.html –

+0

嘗試使用更改按代碼 –

+0

否這是行不通的...我在控制檯中看到,但在複選框單擊事件沒有行動正在觸發,當我檢查我的選擇標籤內的值.. –

0
public function loadSkills() { 
     $this->getServiceArea(); 
     $this->loadModel('Skill'); 
     $skills = array(); 
     if (isset($this->request['data']['id'])) { 
     $ids = explode(",",$this->request['data']['id']); 
     if(count($ids)>1){ 
      $skills = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
      'Skill.service_area_category_id IN' => $ids))); 
      } else { 
      $skills = $this->Skill->find('list', array('fields' => array('Skill.id','Skill.skill_name'),'conditions' => array(
     'Skill.service_area_category_id' => $ids))); 
      }   
     } 
     echo json_encode($skills); 
     exit(); 
    }