|
楼主 |
发表于 2011-2-26 11:47:28
|
显示全部楼层
调用json的视图部分代码如下:
<?php header("Content-type: text/html; charset=utf-8"); ?>
分 类:
<select name="task_category" id="task_category" onchange="get_task(this.value,'<?= site_url()?>youjun/get_task_byid/')">
<option value="0" selected="selected">请选择</option>
<?php foreach ($product_category as $row) :?>
<option value="<?=$row->id?>"><?=$row->catename?></option>
<?php endforeach ?>
</select >
<select name="cateid" id="cateid">
<option value="0" selected="selected">请选择</option>
</select>
<span id="cateidtip" class="red">*</span> <span>只能选择最终分类才能继续发布</span>
<script type="text/javascript" src="<?php echo base_url();?>js/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
function get_task(pid,url){
$.getJSON(url + pid, function(json) {
var str ='<option value="" selected="selected">请选择</option>';
for(var i=0; i <json.length; i++){
str+='<option value="' + json[i].id + '">' + json[i].catename + '</option>';
}
$("#cateid").html(str);
});
}
</script>
响应json的控制器是youjun,代码如下:
class Youjun extends Controller {
function __construct(){
parent::Controller();
$this->load->helper('url');
$this->load->library('json');
$this->load->helper('html');
$this->load->model('product_category_model');
}
function get_task_byid($id){
$data['task'] = $this->product_category_model->get_cate($id);
echo $this->json->encode($data['task']);
}
}
模型product_category_model的get_cate函数:
function get_cate($id)
{
$this->db->where('pid',$id);
$query = $this->db->get('product_category');
if ($query->num_rows() > 0)
{
return $query->result();
}
else {
return false;
}
} |
|