简体   繁体   English

使用从PHP获取的Json对象生成LI

[英]Generate LI with Json Objects get from PHP

I have a little problem. 我有一点问题。 I've got json datas from a $.get() request with jQuery 我从jQuery的$.get()请求中获得了json数据

Here is the output : 这是输出:

[
    {"name":"Silver","price":525,"per_month":"20","first_invoice":"20"},
    {"name":"Gold","price":500,"per_month":"50","first_invoice":"0"},
    {"name":"Avion","price":750,"per_month":"10","first_invoice":"10"}
]

I try to generate an UL with an LI for each objetcs... 我尝试为每个对象生成带有LI的UL ...

I write this little code : 我写这个小代码:

//send get request
    $.get(
        url, 
        { 'dst': dst, 'price':price },
        function(data) {

            var objects = jQuery.parseJSON(data);

            var items = [];

            jQuery.each(objects, function(){
                console.log(this);

                items.push('<li id="' + this.name + '">' + this.price + '</li>');
            }); 

            jQuery('<ul/>', {
                'class': 'my-new-list',
                html: items.join('')
            }).appendTo('#results');                
        }
    );

When i use console.log() each objects are logged by firebug : no error... it works. 当我使用console.log()每个对象都由firebug记录:没有错误...可以正常工作。

This is the items.push() and the appendTo() which generate nothing... no error in the console... no append in my #results div... 这是items.push()appendTo() ,它们什么都不生成...在控制台中没有错误...在我的#results div中没有​​添加...

I'm sure I do something wrong. 我确定我做错了。 Can anyone help me ? 谁能帮我 ?

Update 更新

With the help of Fabrício Matté (answer after), the problem was the iframe. 在FabrícioMatté(后接答案)的帮助下,问题出在iframe上。

jQuery('<ul/>', {
                'class': 'my-new-list',
                html: items.join('')
            }).appendTo(jQuery('body').find('#results'));  

That works =) 可行=)

Sey you soon Stackoverflow Community 很快告诉您Stackoverflow社区

edit: As per João's comment, your original code is working perfectly fine. 编辑:根据João的评论,您的原始代码运行正常。 Make sure that: 确保:

  1. #result is in the DOM when you try to append the data; 当您尝试追加数据时, #result在DOM中;
  2. jQuery.parseJSON(data) returns an array of objects; jQuery.parseJSON(data)返回一个对象数组;
  3. jQuery is properly included and there are no syntax errors before executing that line; 正确包含了jQuery,执行该行之前没有语法错误;
  4. I rewrote the code slightly to make it easier to read as well: 我略微重写了代码,以使其更易于阅读:

     var objects = jQuery.parseJSON(data); var items = ''; jQuery.each(objects, function(){ items += '<li id="' + this.name + '">' + this.price + '</li>'; }); jQuery('<ul/>', { 'class': 'my-new-list' }).html(items).appendTo('#results'); 

Fiddle 小提琴


EDIT 编辑

As per OP's comment, you can't use a selector to select elements inside of an iframe without referencing its document first. 根据OP的评论,如果不先引用iframedocument ,就无法使用选择器来选择iframe元素。 Here's how I'd do it with contents() and find() : 这是我将如何使用contents()find()

//replace the $('iframe') with ID/class selector if possible
jQuery('iframe').contents().find('#results').append(jQuery('<ul/>', {
    'class': 'my-new-list'
}).html(items));

Fiddle 小提琴

Note that this does not work if the iframe document is from a different host, port or protocol. 请注意 ,如果iframe文档来自其他主机,端口或协议,则此方法无效。
Same Origin Policy reference 同源政策参考

$('<ul>')
    .addClass('my-new-list')
    .html($.map(objects, function(el) {
        return '<li id="' + el.name + '">' + el.price + '</li>';
    }).join("\n"))
    .appendTo('#results');

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

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