简体   繁体   English

如何使用jQuery获取JSON编码值

[英]How to get json encoded values using jquery

This is my html where i want to get below json values: 这是我想要在json值以下获取的html:

<div id='bodyText'></div>

Here i have json encoded PHP value for 在这里我有json编码的PHP值

$page->results()

as follows in below jquery items: 如下jquery项所示:

[{name:'abc',id:'10'},
{name:'def',id:'20'}]

And below using jquery code how would i get these above values within div elements with id 'bodyText'. 在下面使用jQuery代码,我将如何在id为“ bodyText”的div元素中获取上述值。

$(document).ready(function(){
            var items = <?php echo json_encode($page->results()); ?>;
            $.each(items, function(key, val) {
                $('bodyText').append($.text(val));
            });

        });

I am unable to get the values please help me to solve the problem... 我无法获取值,请帮助我解决问题...

you are not correct referencing bodyText div id. 您不正确引用bodyText div ID。 Try this 尝试这个

$(document).ready(function(){
    var items = <?php echo json_encode($page->results()); ?>;
    $.each(items, function(key, val) {
        $('#bodyText').append($.text(val));
    });

});

you need to change this $('bodyText').append($.text(val)); 您需要更改此$('bodyText').append($.text(val)); to $('#bodyText').append($.text(val)); $('#bodyText').append($.text(val));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM