Javascript Codeigniter:传递基于ajax的数据
我正在根据所选国家的Javascript Codeigniter:传递基于ajax的数据,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我正在根据所选国家的onchange()事件获取城市数据。 我必须根据所选国家填充城市数据 class ajax extends CI_Controller { public function getcities(){ if($this->input->post('countryId')){ $countryId= $this->input->post('countryId'); } fore
onchange()
事件获取城市数据。
我必须根据所选国家填充城市数据
class ajax extends CI_Controller {
public function getcities(){
if($this->input->post('countryId')){
$countryId= $this->input->post('countryId');
}
foreach ($this->user_model->getselectedcity($this->input->post('countryId')) as $key => $value) {
$cities[] = $value->city;
}
echo json_encode($cities);
}
}
在我看来,我有以下脚本:
<script type="text/javascript">
$(document).ready(function (){
$('#changecountry').change(function(){
//alert($(this).val());
$.ajax({
method: "POST",
url: "<?php echo base_url()?>index.php/ajax/getcities",
data:{countryId:$(this).val()},
success: function(data){
console.log(data);
}
});
});
});
</script>
您需要使用ID属性在城市下拉列表中附加Ajax响应,如:
<script type="text/javascript">
$(document).ready(function (){
$('#changecountry').change(function(){
$.ajax({
method: "POST",
url: "<?php echo base_url()?>index.php/ajax/getcities",
data:{countryId:$(this).val()},
success: function(response){
$.each(response, function (index, value) {
$("#selectID").append("<option value='"+value+"'>" + value + "</option>");
});
}
});
});
});
</script>
$(文档).ready(函数(){
$('#changecountry').change(function(){
$.ajax({
方法:“张贴”,
url:“index.php/ajax/getcities”,
数据:{countryId:$(this.val()},
成功:功能(响应){
$.each(响应、函数(索引、值){
$(“#selectID”).append(“+value+”);
});
}
});
});
});
您的下拉列表(在此处添加ID属性):
<?php
if($value['type']=='cityname'){
if(isset($value['cityname'])){
echo '<div class="form-group">';
echo form_dropdown('cityname',$value['options'],'','class="form-control" id="selectID"');
echo '</div>';
}
}
?>
在控制器中
foreach ($this->user_model->getselectedcity($this->input->post('countryId')) as $option) {
$cities[] = '<option value="'.$option["city"].'">'.$option["city"].'</option>';
}
HTML格式的
<select class="form-control" id="state" name="state">
<option selected="selected">--Select State--</option>
</select>
--选择状态--
foreach ($this->user_model->getselectedcity($this->input->post('countryId')) as $option) {
$cities[] = '<option value="'.$option["city"].'">'.$option["city"].'</option>';
}
success: function(html)
{
$("#state").html(html);
}
<select class="form-control" id="state" name="state">
<option selected="selected">--Select State--</option>
</select>