我正在尝试使用 AJAX 根据另一个下拉菜单值填充下拉菜单。但我无法让它工作。希望你们能指出我正确的方向。
模型:
function get_all_classes($gradeid)
{
$this->db->where('grade_id', $gradeid);
$query = $this->db->get('classes');
$result = $query->result();
foreach($result as $row)
{
$options[$row->id] = $row->name;
}
return $options;
}
看法:
<?php
echo form_dropdown('grades', $grades, '', 'id="grades"');
?>
//This dropdown is filled based on the selection from my 'grades' dropdown menu
<?php
echo form_dropdown('classes', $classes, '', 'id="classes"');
?>
控制器: 函数 registerform() { $this->load->model('school_info_model');
if ($query = $this->school_info_model->get_all_grades())
{
$data['grades'] = $query;
}
if ($query = $this->school_info_model->get_all_classes($this->input->post('gradeid')))
{
$data['classes'] = $query;
}
$data['main_content'] = 'register_form_view';
$this->load->view('template/template.php', $data);
}
阿贾克斯:
var gradeid = $('#grades').val();
$("#grades").change(function(){
$.ajax({
url: '<?php echo base_url().'index.php/login/registerform' ?>',
type: 'POST',
data: gradeid,
succes: function(){
alert('dadasd');
}
});
});
编辑:更新 ajax 代码