对json响应表示怀疑



我有这样的代码:

$.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数组级别的键。

$.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/

你应该这样做:

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

希望这对你有帮助。欢呼声

相关内容

  • 没有找到相关文章

最新更新