weixin_33744854 2014-01-28 11:59 采纳率: 0%
浏览 21

Ajax jQuery JSON调用

I needing some help here if possible. I have a JSON file and I'm trying to get just 1 result from the image files and specify which image by a number e. g. the first one - is this possible. I have tried lots of different [1] ways but I'm clearly doing something wrong here.

Here's my attempt: http://jsfiddle.net/garrycalliopix/Z233A/

$(function(){
    $.get('http://sunrise.bluechipholidays.co.uk/webservices/property/images/apikey/demo/propertycode/zinc52/propertycode/proch', function(data){
    var $imagetest = $('#imagetest');
    $(data.data).each(function(i, x){
        $(x.images).each(function(j, k){
            var $img = $('<img></img>');
            $img.attr[1]; //i want the 2nd image returned
            $img.attr('width', '150px');
            $img.attr('height', '75px');
            $img.attr("src", k.url).appendTo("#imagetest");
        });

    });
});
});

[0] is the image number returned not the count of images: [2] would be the 3rd image.

  • 写回答

1条回答 默认 最新

  • 七度&光 2014-01-28 12:07
    关注

    So you are looking to get the second image? You can get the second image with this small example.

    Example below returns two images, are those images the ones you want or do you want only the first?

    See this fiddle

    $.get('http://sunrise.bluechipholidays.co.uk/webservices/property/images/apikey/demo/propertycode/zinc52/propertycode/proch', function(data){
            var $imagetest = $('#imagetest');
            var j = 0; // keep a counter to count which element you want to display
            $(data.data).each(function(i, x){ // loop through all elements in data.data
                j++; // add one (j = j + 1)
                if(j == 2){ // choose which one you want to display, 2 is the second iteration of $(data.data).each()
                    var $img = $('<img></img>');
                                $img.attr('width', '150px');
                    $img.attr('height', '75px');
                    $img.attr("src", x.images[2].url).appendTo("#imagetest");
                }
            });
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分