首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Json返回html表中jquery中的数组显示

Json返回html表中jquery中的数组显示
EN

Stack Overflow用户
提问于 2016-07-27 08:06:13
回答 1查看 71关注 0票数 1

在CodeIgniter控制器中返回json在下面的格式,

代码语言:javascript
代码运行次数:0
运行
复制
{"r":[{"galleryid":"1","gname":"birthday","eventdate":"2016-07-20 00:00:00","totalphoto":"250","selectedphoto" :"100","glock":"0","userid":"1"},{"galleryid":"2","gname":"anniversary","eventdate":"2016-07-14 00:00:00","totalphoto":"500","selectedphoto":"251","glock":"0","userid":"1"}]}

并以代码形式从控制器返回:

代码语言:javascript
代码运行次数:0
运行
复制
     $this->load->model('gallery_model');
    $data['r'] = $this->gallery_model->gallery_data($userid);
    echo json_encode($data);

但是在视图中它只是显示:未定义的

查看代码:

代码语言:javascript
代码运行次数:0
运行
复制
<script type="text/javascript">

// Ajax post
$(document).ready(function () {
    $("input#displaygallery").on('click', function (event) {
        //var user_id = document.getElementById("userdropdown").value;
        //alert(user_id);

        event.preventDefault();
        var user_id = document.getElementById("userdropdown").value;
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>" + "admin_cont/gallery_controller/user_userdata",
            dataType: 'json',
            data: {userid: user_id},
            success: function (res) {
                //console.log( res );

                $.each(res, function (idx, obj) {
                    alert(obj.tagName);
                });
            }
        });
    });
});

</script>
EN

回答 1

Stack Overflow用户

发布于 2016-07-27 08:26:47

尝试:

javascript:

代码语言:javascript
代码运行次数:0
运行
复制
$("input#displaygallery").on('click', function (event) {
    event.preventDefault();
    var user_id = document.getElementById("userdropdown").value;
    jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "admin_cont/gallery_controller/user_userdata",
        dataType: 'json',
        data: {userid: user_id},
        success: function (res) {
            var html = "<table>";
            html += "<thead>";

            html += "<tr>";
            html += "<th>eventdate</th>";
            html += "<th>galleryid</th>";
            html += "<th>glock</th>";
            html += "<th>gname</th>";
            html += "<th>selectedphoto</th>";
            html += "<th>totalphoto</th>";
            html += "<th>userid</th>";
            html += "<tr>";

            html += "<thead>";
            html += "<tbody>";
            for (i = 0; i <= res.r.length - 1; i++) {
                html += "<tr>";
                html += "<td>" + res.r[i].eventdate + "</td>";
                html += "<td>" + res.r[i].galleryid + "</td>";
                html += "<td>" + res.r[i].glock + "</td>";
                html += "<td>" + res.r[i].gname + "</td>";
                html += "<td>" + res.r[i].selectedphoto + "</td>";
                html += "<td>" + res.r[i].totalphoto + "</td>";
                html += "<td>" + res.r[i].userid + "</td>";
                html += "<tr>";
            }
            html += "</tbody>";
            html += "</table>";
            $("your cointainer id or class name").html(html);
        }
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38607255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档