依赖性下拉不起作用

时间:2013-09-23 10:16:18

标签: javascript php jquery codeigniter drop-down-menu

在下面的代码中,我在一个下拉列表中有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>

1 个答案:

答案 0 :(得分: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;
}