我无法让 ajax 与我的“动态组合框”一起工作。首先是代码:视图:
<?php echo form_open('control_form/add_all'); ?>
<label for="make">State<span class="red">*</span></label>
<select id="make" name="make" >
<option value=""></option>
<?php
foreach($makeOptions->result() as $make){
echo '<option value="' . $make->make_id . '">' . $make->make . '</option>';
}
?>
</select>
<label for="model">City<span class="red">*</span></label>
<!--this will be filled based on the tree selection above-->
<select id="model" name="model">
<option value=""></option>
</select>
<label for="f_membername">Member Name<span class="red">*</span></label>
<!--<input type="text" name="f_membername"/>-->
<?php echo form_close(); ?>
视图控制器:
function dropDown(){
if ($this->ion_auth->requireAdmin())
$data = array('title' => 'DropDown', 'main_content' => 'admin/dropDown');
$data['makeOptions'] = $this->vehicle_model->getMake(Null, Null);
$data['modelOptions'] = $this->vehicle_model->getModel(Null, Null, NULL, Null);
$this->load->view('admin/includes/template', $data);
}
Ajax 控制器:
function get_model($make){
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->vehicle_model->getModelByMake ($make)));
}
jQuery:
$('#model').hide();
$('#make').change(function(){
var make_id = $('#make').val();
if (make_id != ""){
var post_url = "http://pulsedrivers.com/admin/get_model/" + make_id;
$.ajax({
type: "POST",
url: post_url,
success: function(models) //we're calling the response json array 'cities'
{
$('#model').empty();
$('#model').show();
$.each(models,function(model_id,model)
{
var opt = $('<option />'); // here we're creating a new select option for each group
opt.val(model_id);
opt.text(model);
$('#model').append(opt);
});
} //end success
}); //end AJAX
} else {
$('#model').empty();
$('#model').hide();
}//end if
}); //end change
模型(由 get_model 控制器使用):
function getModelByMake ($make, $tree = null){
$this->db->select('model_id, model, make_id');
if($tree != NULL){
$this->db->where('make_id', $make);
}
$this->db->where('make_id', $make);
$query = $this->db->get('model');
$models = array();
if($query->result()){
foreach ($query->result() as $model) {
$models[$model->model_id] = $model->model;
}
return $models;
} else {
return FALSE;
}
}
该视图包含两个下拉列表,一个显示品牌,另一个显示相应的模型。选择品牌后,模型不显示。Firebug 在此过程中不显示任何错误。
事情已经检查:jquery 工作,脚本加载。
控制器 get_models 返回一个像 1:Corvette, 2:Camaro 这样的数组,因此它看起来也可以正常工作。
任何帮助将不胜感激,谢谢!