Javascript “从属”下拉列表不起作用
在下面的代码中,我在一个下拉列表中有两个下拉列表,我从课程主题表中获取课程代码,在另一个下拉列表中,应显示课程主题表中所选课程代码的相关主题代码。但是我无法获得相关的下拉主题代码。请任何人帮助我 控制员:学生站Javascript “从属”下拉列表不起作用,javascript,php,jquery,codeigniter,drop-down-menu,Javascript,Php,Jquery,Codeigniter,Drop Down Menu,在下面的代码中,我在一个下拉列表中有两个下拉列表,我从课程主题表中获取课程代码,在另一个下拉列表中,应显示课程主题表中所选课程代码的相关主题代码。但是我无法获得相关的下拉主题代码。请任何人帮助我 控制员:学生站 function search_by_course() { $this->load->model('subject_model'); $id = $this->input->post('subject_id'); //get your data fr
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));
}
视图:学生\详细信息\视图
<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>
$(函数(){
$(“#课程(代码)id”).live(“更改按键”,函数(){
var-id=0;
id=$(this.val();
如果($(this.val()!=''){
$.post(“”,
{
受试者id:id
},函数(数据){
$(“.subject”).html(数据['html_字符串]);
},“JSON”
);
}
});
});
要做的更改:鉴于:
$(函数(){
$(“#课程(代码)id”).live(“更改”,函数(){
var id=$(this.val();
如果(id!=''){
$.ajax({
url:“”,
键入:“POST”,
数据:{'subject_id':id},
成功:功能(resp){
如果(resp!=“”){
$(“#主题#代码#id”).html('resp');
}否则{
警报(“无响应!”);
}
},
错误:函数(resp){
警报(“错误!”);
}
});
}
});
});
在控制器中:
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;
}
按课程搜索函数(){
$this->load->model('subject_model');
$id=$this->input->post('subject_id');
//从模型中获取数据
$res\u subject=$this->subject\u model->subject\u list($id);
$html_string=“”;
//$html_字符串=“”;
对于($x=0;$xsubject\u id.“>”$res\u subject[$x]->subject\u name.“;
}
//$html_字符串=“”;
//echo json_encode(数组('html_string'=>$html_string));
echo$html_字符串;
}
课程代码下拉列表未调用javascript函数@Nil'zDo您的控制台中是否有js错误?请检查您的控制台
,并通知我。
<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;
}