0
値が他のものに依存する2つのドロップダウンメニューを設定しようとしています。合計で私は3つのドロップダウンを持っています。 1つはクラスを選択するためのものであり、他の2つは選択されたクラスの科目および試験を選択するためのものである。 私は1つのドロップダウンメニュー(例えば:select subjectのように)を設定することができます。どのように私はそれらの両方を設定することができます。次のように私のコードは次のとおりです。1つの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がドロップダウンリストに件名リストを表示しています。しかし、私は
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));
}
この
のような1つの以上のJSONオブジェクトを渡したいどのように対応するドロップダウンで、この表示試験名からiループすることができます。これは、[[{"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":""}]]
私のコンソールプレビューです。 、あなたにも二番目の配列をループする必要があります
されてくださいありがとうございました:)助けるために – Ajzz
ハッピー! –