2011-07-08 114 views
1

我有這樣的代碼:懷疑與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}] 

我想for循環做一個與JSON,其中+registro.id+是ID和+registro.i+是接收值第一個json數組級別的鍵。

+1

發佈時請使用源代碼格式,以便人們可以理解您的代碼。 –

回答

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/

+0

我忘了一個細節,我需要添加圖像類imagelink w16,我該怎麼做? – Fernando

+0

@Fernando,除了'src',你可以用這個語法設置各種屬性(用','*分隔)。更新了答案和jsfiddle來反映這一點。 –

+0

謝謝。你幫了我很多。 – Fernando

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

希望這有助於。歡呼聲