繁体   English   中英

从属下拉列表不起作用

[英]Dependent drop down is not working

在此下面的代码中,我有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>

要做的更改:
鉴于:

<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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM