2017-04-15 49 views
0

我想填充兩個下拉菜單,其值與另一個依賴。總共有3個下拉菜單。一個選擇課程,另外兩個選擇課程的科目和考試。 我可以填充一個下拉菜單(比如說:選擇主題)。但我如何填充他們兩個。我的代碼如下:用一個php函數返回多個json對象

<table> 
     <tr> 
      <td> select class</td> 
     </tr> 
     <tr> 
      <td> select subject</td> 
     </tr> 
     <tr> 
      <td> select exam</td> 
     </tr> 
     <tr> 
     <td><select class="form-control m-bot15" name="class_id" value='' onchange="myFunction(this)" style="float:left;" id="carId"> 
      <option value="">select a class</option> 
      <option value="1">One</option> 
      <option value="2">Two</option> 
      <option value="3">Three</option> 
      </select></td> 
     </tr> 
     <tr> 
     <td><select class="form-control m-bot15" name="subject_id" value='' style="float:left;" id="emptyDropdown"> 
      <option value="">select a subject</option> 
      </select></td> 
      <tr> 
     </tr> 
     <td><select class="form-control m-bot15" name="exam_id" value='' style="float:left;" id="emptyDropdown2"> 
      <option value="">select a subject</option> 
      </select></td> 
      </tr> 
     </table> 

這是我的看法功能我來填充主題名稱腳本是:

<script> 
     function myFunction(obj) 
      { 
       $('#emptyDropdown').empty() 
       var dropDown = document.getElementById("carId"); 
       var carId = dropDown.options[dropDown.selectedIndex].value; 
       $.ajax({ 
        type: "POST", 
         dataType: 'json', 
         url: "<?php echo base_url();?>mark/newdrp", 
         data: { 'carId': carId }, 
         success: function(data){ 

          $.each(data, function() { 
          $('#emptyDropdown').append('<option value='+this.subject_id+'>' + this.name + '</option>'); 
         }); 
        } 
       }); 
       } 
     </script> 

我的控制器功能newdrp是

public function newdrp(){ 
     $classId = $this->input->post('carId'); 
     $subjects = $this->subject_model->Subjectbyclassid($classId); 
     echo json_encode($exams); 
    } 

這工作得很好。 Iam在我的下拉列表中獲取主題列表。但我想通過一個更JSON對象,這樣

 public function newdrp(){ 
     $classId = $this->input->post('carId'); 
     $subjects = $this->subject_model->Subjectbyclassid($classId); 
     $exams = $this->exam_model->Exambyclassid($classId); 
     echo json_encode(array($subjects,$exams)); 
    } 

這是我的控制檯預覽

[[{"subject_id":"1","name":"Physics","class_id":"1","teacher_id":null},{"subject_id":"2","name":"Chemistry","class_id":"1","teacher_id":null},{"subject_id":"3","name":"Mathematics","class_id":"1","teacher_id":null}],[{"exam_id":"22","name":"BW9","date":"03\/10\/16","class_id":"1","comment":""},{"exam_id":"26","name":"BW10","date":"17\/10\/16","class_id":"1","comment":""},{"exam_id":"30","name":"BW11","date":"31\/10\/16","class_id":"1","comment":""},{"exam_id":"34","name":"BW12","date":"14\/11\/16","class_id":"1","comment":""},{"exam_id":"40","name":"BW13","date":"28\/11\/16","class_id":"1","comment":""},{"exam_id":"45","name":"BW14","date":"11\/12\/16","class_id":"1","comment":""},{"exam_id":"46","name":"Revision Exam 1","date":"02\/01\/17","class_id":"1","comment":""},{"exam_id":"49","name":"Revision Exam 2","date":"8\/01\/2017","class_id":"1","comment":""},{"exam_id":"51","name":"Revision Exam 3","date":"15\/01\/17","class_id":"1","comment":""},{"exam_id":"55","name":"Revision Exam 4","date":"22\/01\/17","class_id":"1","comment":""},{"exam_id":"57","name":"Revision exam 5","date":"26\/01\/2017","class_id":"1","comment":""},{"exam_id":"59","name":"Revision Exam 6","date":"29\/01\/17","class_id":"1","comment":""}]] 

我怎麼可以遍歷這一個顯示在相應的下拉列表中考試名稱。請幫助

回答

1

你必須遍歷第二個數組爲好,data是父陣列

success: function(data){ 
    var examArr = data[1]; 
    $.each(examArr, function() { 
    $('#emptyDropdown2').append("<option value='" + $(this).exam_id + "'>" + $(this).name + "</option>"); 
    }); 
} 
+0

謝謝你這麼多:) – Ajzz

+0

樂意提供幫助! –

相關問題