jquery if语句,当日期不匹配时显示'default'

时间:2013-03-01 15:18:46

标签: jquery

我有一个像这样的json文件:

{
  "video": {
    "default" : {
      "url": "http://showmms.rd.llnwd.net/d4/u/video/mezzanine/mov/1002742_1_0_trl02.mov",
      "imgSm": "http://www.sho.com/assets/vendor/sxsw/img/iphone-video.png",
      "imgLg": "http://www.sho.com/assets/vendor/sxsw/img/video-img.jpg",
      "imgTitle": "http://www.sho.com/assets/vendor/sxsw/img/video-title.jpg"
    },
    "mar8" : {
      "url": "http://showmms.rd.llnwd.net/d4/u/video/mezzanine/mov/1002742_1_0_trl02.mov",
      "imgSm": "http://www.sho.com/assets/vendor/sxsw/img/promos/SXSW2013_VOD_Banners_Dexter_Small_320x41.png",
      "imgLg": "http://www.sho.com/assets/vendor/sxsw/img/promos/SXSW2013_VOD_Banners_Dexter_Large_320x264.png",
      "imgTitle": "http://www.sho.com/assets/vendor/sxsw/img/video-title.jpg"
    },
    "mar9" : {
      "url": "http://showmms.rd.llnwd.net/d4/u/video/mezzanine/mov/1002742_1_0_trl02.mov",
      "imgSm": "http://www.sho.com/assets/vendor/sxsw/img/iphone-video.png",
      "imgLg": "http://www.sho.com/assets/vendor/sxsw/img/video-img.jpg",
      "imgTitle": "http://www.sho.com/assets/vendor/sxsw/img/video-title.jpg"
    }
  }
}

}

因此,根据日期,它应显示正确的图像和网址。但如果它与日期不匹配,它应该使用“默认”,我的jquery不工作,但我不确定原因:

var today = new Date();
var dd = today.getDate();
var promo = 'mar' + dd;

var imgLg = data.video[promo].imgLg;
var imgSm = data.video[promo].imgSm;
var url = data.video[promo].url;

promo = data.video[promo] ? promo : 'default';

$("img.video-lg").attr("src",imgLg);
$("img.video-sm").attr("src",imgSm);    
$("a.play-btn").attr("href",url);

如果日期匹配,则有效,但如果没有,则不会引入“默认”

3 个答案:

答案 0 :(得分:3)

可能dd将显示例如09而不仅仅是9你想要的。

答案 1 :(得分:1)

var today = new Date();
var dd = today.getDate();
var promo = 'mar' + dd;

if(data.video[promo] === undefined)
  promo = 'default';

var imgLg = data.video[promo].imgLg;
var imgSm = data.video[promo].imgSm;
var url = data.video[promo].url;

$("img.video-lg").attr("src",imgLg);
$("img.video-sm").attr("src",imgSm);    
$("a.play-btn").attr("href",url);

答案 2 :(得分:0)

如果您尝试调试代码,VAR dd将仅返回DAY。您必须先格式化日期。