【发布时间】:2015-07-25 06:40:01
【问题描述】:
我需要从下拉列表中获取所选国家 ID 的国家代码
我有一个 ajax 调用,它返回一个 json 数据:
function fetchCountryCode(sel) {
var country_id = sel;
var data = "country_id=" + country_id
// var baseurl =
alert(country_id);
$("#code").html("");
if (country_id.length > 0) {
$.ajax({
type: "POST",
url: "<?php echo base_url();?>index.php/register/code",
data: "country_id=" + country_id,
dataType: 'json',
success: function(html) {
$("#code").html(html);
}
});
}
}
控制器函数解析数据:
public function code($countryId)
{
// Get countries list
$cId = $_POST['country_id'];
//echo "<pre>"; print_r($cId); exit;
$code = file_get_contents($this->config->config['api_url'] . 'countries');
$rawdata = json_decode($code);
// echo "<pre>"; var_dump($rawdata);
foreach($rawdata as $item){
print_r($item->code);
}
exit;
$data['countries'] = $this->load->view('login/code', $code);
}
它为我提供了所有国家/地区的数据,但我只需要获取所选 ID 的数据。我是新手,请指导
【问题讨论】:
标签: jquery json codeigniter