下拉列表无法控制第二个字段。我不确定是什么问题。
我的表包含:
表1:
car (brand_id , car_brand)
表2:
model (model_id , brand_id , car_model)
这是我在Controller上的代码。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Front extends CI_Controller {
public function myformAjax($id) {
$result = $this->db->where("brand_id",$id)->get("model")->result();
echo json_encode($result);
}
}?>
这是我在View上的代码。
<select name="sel_brands" id="sel_brands" class="form-control">
<option value="" selected="selected">Brand</option>
<?php foreach ($car as $count): ?>
<option value="<?php echo $count->brand_id; ?>"><?php echo $count->car_brand; ?></option>
<?php endforeach; ?>
</select>
<select name="sel_model" id="sel_model" class="form-control">
<option selected="selected" value=""> Model </option>
</select>
<script type="text/javascript">
$(document).ready(function() {
$('select[name="sel_brands"]').on('change', function() {
var brandID = $(this).val();
if(brandID) {
$.ajax({
url: '<?php echo base_url();?>front/onlinecheck/ajax/'+brandID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="sel_model"]').empty();
$.each(data, function(key, value) {
$('select[name="sel_model"]').append('<option value="'+ value.model_id +'">'+ value.car_model +'</option>');
});
}
});
}else{
$('select[name="sel_model"]').empty();
}
});
});
</script>