我尝试使用AJAX根据另一个下拉菜单值填充下拉菜单。但我无法让它发挥作用。希望你们能指出我正确的方向。
MODEL:
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() { $这 - &GT;负载&GT;模型(&#39; school_info_model&#39);
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);
}
AJAX:
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代码
答案 0 :(得分:0)
看起来你需要某种类型的Ajax绑定事件,如下所示:
$('#link').on('click', function(){
$.ajax({
url: '<?php echo base_url().'index.php/login/registerform' ?>',
type: 'POST',
data: gradeid,
});
});
答案 1 :(得分:0)
您可能希望在更改中设置您的成绩单,以便每当有人更改该字段时它都会更改,因为您将动态设置其他下拉列表...然后您将需要根据您更改选项retrive ...所以你需要回调ajax返回的时候......你也应该对错误等有所了解,但你可以在这里阅读更多关于http://api.jquery.com/jQuery.ajax/
的信息$("#grades").change(function(){
var gradeid = $(this).val();
$.ajax({
url: '<?php echo base_url().'index.php/login/registerform' ?>',
type: 'POST',
data: gradeid
}).done(function(data) {
//set your returned data to your options for classes
});
});