$("#panchayat").change(function(){
var param = {'panchayat_id':$(this).val()};
alert (param['panchayat_id']);
$.ajax({
type : 'POST',
url : '<?php echo base_url();?>index.php/parent_taluk',
dataType : 'html',
data: param,
success : function(data)
{
var taluk_id='taluk_'+data;
alert(taluk_id);
document.getElementById(taluk_id).selected=true;
},
error : function(XMLHttpRequest, textStatus, errorThrown)
{ alert(XMLHttpRequest.responseText);
}
});
});
public function parent_taluk(){
$this->load->helper('form');
$Panchayat_Id= $this->input->post('panchayat_id');
$this->check_isvalidated();
$this->load->model('admin_model');
$data['taluk'] = $this->admin_model->parent_taluk_one($Panchayat_Id);
$taluk_id=$data['taluk']->Taluk_Id;
$taluk_name=$data['taluk']->Taluk_Name;
echo $taluk_id;
}
返回的数据是一个数组:
Array
(
[0] => Array
(
[Panchayat_Id] => 1050
[Taluk_Id] => 47
[Panchayat_Name] => Municpality
[Taluk_Name] => Eranad
)
)
47
为taluk_id返回的值是一个数组
taluk_Array
(
[0] => Array
(
[Panchayat_Id] => 1050
[Taluk_Id] => 47
[Panchayat_Name] => Municpality
[Taluk_Name] => Eranad
)
)
47
我需要[Taluk_Id] =&gt; 47,这是从这个返回值获得的47,或者我只需要47作为返回值。
我无法从控制器获取返回到ajax函数的值47。请帮忙
答案 0 :(得分:0)
假设ajax返回一个对象数组。每个对象都是一个taluk对象,其中包含Taluk_Id,Taluk_Name,Panchayat_Id和Panchayat_Name等属性。以下是可行的。
var res = taluk_Array.filter(function(taluk){
return taluk.Taluk_Id===47;
});
var result = res[0];
现在打印此
result.Taluk_Id
result.Taluk_Name
result.Panchayat_Id
result.Panchayat_Name
在另一个组合框中设置此结果Taluk_Id
var element = document.getElementById('parentSelectCombo');
element.value = result.Taluk_Id; //47 in your case