0

在下面的代码中,我在一个下拉列表中有 2 个下拉列表,我从课程主题表中获取课程代码,在另一个下拉列表中,应显示课程主题表中所选课程代码的相关主题代码。但我无法获得相关的下拉主题代码。请任何人帮助我。

控制器:student_site

function search_by_course()
{
  $this->load->model('subject_model');
  $id = $this->input->post('subject_id');

  //get your data from model
  $res_subject = $this->subject_model->subject_list($id);
  $html_string = "";
  $html_string .= "<select id='subject_code_id'>";
  for($x=0;$x<count($res_subject);$x++)
  {
     $html .= "<option id=".$res_subject[$x]->subject_id.">".$res_subject[$x]->subject_name."</option>";
  }
  $html_string .= "</select>";

  echo json_encode(array('html_string'=>$html_string));
}

型号:student_model

function subject_list($id)
    {
        //echo "exam_name inside get_subject_records".$exam_name;
        //$this->db->select('course_code,subject_code');
        //$this->db->where('exam_name',$exam_name);
        $this->db->where('course_code',$course_name);
        $query = $this->db->get('coursesubject');
        return $query->result();
    }

视图:student_detail_view

<td >

<?php 

        $js = 'class="dropdown_class" id="course_code_id'.$row->id.'"    '; 
        $js_name = 'course_code_id'.$row->id;
        echo form_dropdown($js_name, $data, $row->course_code, $js);

?>

</td>

<td>

<div class="subject"></div>
</td>

<script>
$(function(){
    $("#course_code_id").live("change keypress",function(){
       var id = 0;
       id = $(this).val();
       if( $(this).val() !==''){           
          $.post('<?php echo site_url('student_site/search_by_course') ?>',
            {
                subject_id: id
            },function(data){
                $(".subject").html( data['html_string']);

            },"JSON"
          );
       }
   });
});
</script>
4

1 回答 1

0

要做的改变:
鉴于:

<div class="subject">
    <select id="subject_code_id"></select>
</div>

<script>
$(function(){
    $("#course_code_id").live("change", function(){
       var id = $(this).val();
       if( id != ''){           
          $.ajax({
                url     : '<?=base_url('student_site/search_by_course')?>',
                type    : 'POST',
                data    : { 'subject_id' : id },
                success : function(resp){
                    if( resp != "" ){
                        $("#subject_code_id").html('resp');
                    }else{
                        alert("No response!");
                    }
                },
                error   : function(resp){
                    alert("Error!");
                }
          });
       }
   });
});
</script>

在控制器中:

function search_by_course(){
    $this->load->model('subject_model');
    $id = $this->input->post('subject_id');

    //get your data from model
    $res_subject = $this->subject_model->subject_list($id);
    $html_string = "";
    //$html_string .= "<select id='subject_code_id'>";
    for($x=0;$x<count($res_subject);$x++)
    {
     $html .= "<option id=".$res_subject[$x]->subject_id.">".$res_subject[$x]->subject_name."</option>";
    }
    //$html_string .= "</select>";

    //echo json_encode(array('html_string'=>$html_string));
    echo $html_string;
}
于 2013-09-23T10:46:03.097 回答