我想在codeigniter中创建多个链式下拉列表,在第一个下拉列表中它正常工作,但我想在第二个组合框后选择另一个下拉列表。如何编写第二个下拉列表的ajax,这样我就可以得到第三个下拉结果。
我的模特
function loadcaryear() {
$query = $this->db->query("SELECT DISTINCT(year) FROM drv_master_car_brand");
return $query->result();
}
function loadcarmake($year) {
$query = $this->db->query("SELECT DISTINCT(make) FROM drv_master_car_brand where year = '{$year}' ");
return $query->result();
}
function loadmodelfrombrand($make) {
$query = $this->db->query("SELECT model FROM drv_master_car_brand WHERE make = '{$make}'");
return $query->result();
}
我的控制器。
function carbyyear() {
$arrCaryear = $this->modelRegister->loadcaryear();
foreach ($arrCaryear as $caryears) {
$arrcaryear[$caryears->year] = $caryears->year;
}
$data['year'] = $arrcaryear;
$this->load->view('car',$data);
}
function ajax_car_make() {
if (isset($_POST) && isset($_POST['year'])) {
$year = $_POST['year'];
$arrMakes = $this->modelRegister->loadcarmake($year);
//print_r($arrModels);
foreach ($arrMakes as $makes) {
$arrmakes[$makes->make] = $makes->make;
}
$data['make'] = $arrmakes;
print form_dropdown('make',$arrmakes);
} else {
redirect('site');
}
}
我的观点。
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#yearcombox select').change(function () {
var selYear = $(this).val();
console.log(selYear);
$.ajax({
url: "ajax_car_make",
async: false,
type: "POST",
data: "year="+selYear,
dataType: "html",
success: function(data) {
$('#makecombox').html(data);
},
})
});
});
</script>
</head>
<body>
<div id="mydoubts">
<div id="yearcombox"><?php echo form_dropdown('year',$year, 'onChange="showState(this);"'); ?></div>
<div id="makecombox"></div>
</div>
</body>
</html>
答案 0 :(得分:0)
只需将您的jquery AJAX部分的数据更改为
即可data: {"year":selYear},
希望它能奏效!
谢谢!