我正在使用CodeIgniter,并且有一个带有2个选择选项的表格。第一个选择选项是“汽车制造”,第二个选择选项是“制造”。如果我从“宝马”中选择“汽车制造商”,则第二个选择选项中的值应该会更改并显示所有宝马制造的模型。
**WelcometoDemoCar.php (View)**
*//to get the Car Make List Box*
<input type = "text" name = "car_list" list="car_dalalist" id = "car_list" class = "inktext inklarge" placeholder = "Type of Car" required = "" autocomplete="off" />
<datalist id="car_dalalist">
<?php foreach($carlist as $row_carlist){?>
<?php //echo $row_carlist->Make . " " .$row_carlist->Model ." " .$row_carlist->Year ;?>
<option value="<?php echo $row_carlist->Make;?>"> <?php echo $row_carlist->Make;?></option>
<?php }?>
</datalist>
*//to get the value in the Make Select List Box*
<input type = "text" name = "car_model" list="car_model_dalalist" id = "car_model" class = "inktext inklarge" placeholder = "Car Model" required = <datalist id="car_model_dalalist">
<?php foreach($carModel as $row_carModel){?>
<?php //echo $row_carlist->Make . " " .$row_carlist->Model ." " .$row_carlist->Year ;?>
<option value="<?php echo $row_carModel->Model;?>"><?php echo $row_carModel->Model;?> </option>
<?php }?>
</datalist>
**Welcome.php (Controller)**
$this->data['carlist'] = $this->PostModel->getCarDetails();
$this->data['carModel'] = $this->PostModel->getCarModel();
**PostModel.php (Model)**
*//to get car make*
function getCarDetails(){
$this->db->Distinct();
$this->db->select("Make");
$this->db->from('carlist');
$carListquery = $this->db->get();
return $carListquery->result();
}
*// to get car model*
function getCarModel(){
$make = $this->input->post('car_list');
$this->db->Distinct();
$this->db->select("Model");
$this->db->from('carlist');
$this->db->where('Make' . $make);
$carmodelquery = $this->db->get();
return $carmodelquery->result();
}
public function get_data()
{
$value = $this->input->post("value");
$data = $this->PostModel->get_data($value);
$option ="";
foreach($data as $d)
{
$option .= "<option value='".$d->id."' >".$d->Model."</option>";
}
echo $option;
}
我尝试使用ajax在各种站点上发布了很少的解决方案,但是我认为我的价值观并没有发布到控制器上。
ajax代码
$("#car_list").on("change",function(){
var value = $(this).val();
$.ajax({ url : "welcome/get_data",
type: "post",
data: {"value":'value'},
success : function(data){
$("#car_model").html(data);
},
});
});
真的很感谢您的时间和帮助。
谢谢。
答案 0 :(得分:1)
关于代码,有几个问题。
供将来参考:请参阅OP的评论
主要问题在于点击处理程序:
$("#car_list").on("change",function(){
var value = $(this).val();
$.ajax({ url : "welcome/get_data",
type: "post",
data: {"value":value}, //OP originally used single quotes on the value therefore passing a string instead of the actualy variable
success : function(data){
$("#car_model").html(data);
},
});
});
与控制器和型号有关
public function get_data()
{
$data = $this->PostModel->get_data(); //OP originally passed $value to the model but $value does not exist
$option ="";
if(count($data) > 0){
foreach($data as $d)
{
$option .= "<option value='".$d->Model."' >".$d->Model."</option>";
}
echo $option;
}
}
答案 1 :(得分:0)
请用data: {"value":'value'},
更新data: {"value":value}
(从值中删除单引号)