显示解析json到单选按钮

时间:2014-08-20 05:04:50

标签: javascript php jquery json codeigniter

我想循环json的数据并显示到html上的单选按钮,

var link = "http://localhost/codeigniter/load";
var selector = "#result-media-uploader";
$.getJSON(link,function(data){
        $.each(data,function(i,item){
            var openTag = "<div class='col-lg-2'>";
            var closeTag = "</div>";

            openTag += "<input id='media-radio' type='radio' name='media-radio' value='"+item.id+"'>";
            openTag += "<label class='label'><img width='100%' height='100%' src='"+item.post_content+item.post_title+"'></label>";
            openTag += closeTag;

            $(selector).append(openTag);
        });
});

请求被发送但输出中没有.. 这是服务器代码

$data = $this->db->where('post_type','attachment')->where('post_mine_type','image')
        ->get('cs_posts')->result_array();
        return json_encode($data);

我是javascript的新手,你能解释我的错误吗...感谢您的帮助..

2 个答案:

答案 0 :(得分:0)

试试这个,

    openTag += "<label class='label'><img width='100%' height='100%' src='"+item.post_content+""+item.post_title+"'></label>";
    $("+selector+").append(openTag);

答案 1 :(得分:0)

谢谢你们..(y)

这是最终的代码..

js文件     var folder =“/ codeigniter”;     var url = window.location.protocol +“//”+ window.location.host + folder +“/ admin / uploads / load”;

$('#refresh-media-uploader').click(function(){
    $.getJSON(url,function(data){
        $.each(data,function(i,item){
            var openTag = "<div class='row'><div class='col-lg-2'>";
            var closeTag = "</div></div>";

            openTag += "<input id='media-radio' type='radio' name='media-radio' value='"+item.id+"'>";
            openTag += "<label class='label'><img width='100%' height='100%' src='"+item.post_content+"100x100-"+item.post_title+"'></label>";
            openTag += closeTag;

            $("#result-media-uploader").append(openTag);
        });
    });
});

和sirver方

$data = $this->db->where('post_type','attachment')->where('post_mine_type','image')
        ->get('cs_posts')->result_array();
        echo json_encode($data);