对json的回应表示怀疑

时间:2011-07-08 23:47:06

标签: jquery arrays json variables multidimensional-array

我有这段代码:

$.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+'\', \'_blank\', \'width=750,height=550,scrollbars=no,status=yes\' );" />'; 
        $this.parent().find("#conteudoanexo").html(imghtml,join('')); 
});

返回的json:

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

我想用json接收的值做一个for循环,其中+registro.id+是id而+registro.i+是第一个json数组级别的键。

2 个答案:

答案 0 :(得分:1)

$.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);
        }); 
    });

演示http://jsfiddle.net/gaby/GCzxS/1/

答案 1 :(得分:0)

你应该这样做:

$.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('')); 
});

希望这会有所帮助。干杯