开发者

Doubt with json response

I have this code:

$.getJSON('../encomendasanexos.php?campo=idencomendas&nome=imagem&id='+id_int,
     function(registro){ 
        var imghtml = []; 
        imghtml = '<img src="icones/editar.png" 
          onclick="window.open(\'editImagem.php?
          tabela=anexos&id='+registro.id+'&ordem='+registro.i+'\', 开发者_Python百科\'_blank\', \'width=750,height=550,scrollbars=no,status=yes\' );" />'; 
        $this.parent().find("#conteudoanexo").html(imghtml,join('')); 
});

And the returned json:

[{"id":"400","img":"..\/imagens\/encomendas\/aspire_1309790504.jpg"}, 
{"id":"401","img":"..\/imagens\/encomendas\/casa_1309790507.jpg"},   
{"id":"402","img":null}]

I wanna do a for loop with values received by json, where +registro.id+ is id and +registro.i+ is a key in 1st json array level.


$.getJSON('../encomendasanexos.php?campo=idencomendas&nome=imagem&id='+id_int,
     function(registro){
        var container = $this.parent().find("#conteudoanexo").empty();
        $.each(registro, function(idx, item ){
            var img = $('<img>', { 
                               'src'  : 'http://www.usedprice.com/images/button_edit_grey.gif',
                               'class': 'imagelink w16'
                });
            img.click(function(){
                window.open('editImagem.php?tabela=anexos&id='+item.id+'&ordem='+item.i, '_blank', 'width=750,height=550,scrollbars=no,status=yes');
            });
            container.append(img);
        }); 
    });

demo at http://jsfiddle.net/gaby/GCzxS/1/


You should do:

$.getJSON('../encomendasanexos.php?campo=idencomendas&nome=imagem&id='+id_int,
     function(registros){ 
        var imghtml = []; 
        for(var i=0; i<registros.length; i++){
            var registro = registros[i];
            imghtml.push('<img src="icones/editar.png" 
          onclick="window.open(\'editImagem.php?
          tabela=anexos&id='+registro.id+'&ordem='+i+'\', \'_blank\', \'width=750,height=550,scrollbars=no,status=yes\' );" />');     
        }

        $this.parent().find("#conteudoanexo").html(imghtml,join('')); 
});

Hope this helps. Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜