使用从单击按钮生成的ID来访问JSON对象

时间:2015-12-27 18:41:35

标签: jquery json

我正在尝试编写一个函数,允许我在按钮单击时动态地将内容附加到模态。该函数获取被单击的按钮的ID,然后我希望它使用该ID访问JSON对象以提取要放入模态的信息。我试图使用indexOf来获取感兴趣的JSON对象的索引,但它返回的索引为-1。我很困惑。请帮忙!

这是JS

$(document).ready(function(){

getIdMoreInfo();

  function getIdMoreInfo(){
    $(".more-info").on("click",function(){
      var exerciseId = $(this).attr("id");
      console.log(exerciseId);
      $('.modal-trigger').leanModal({
        dismissible: true, // Modal can be dismissed by clicking outside of the modal
        });
      $.each(workoutInfo.id, function(){
        var indexOfId = workoutInfo.id.indexOf(exerciseId);
        console.log(exerciseId);
        console.log(workoutInfo.id)
        console.log(indexOfId);

      });

    });
  };

});

这是JSON

var workoutInfo = {
"id": [
    {
        "barbellTricepsExtension": [
            {
                "exercise": "Barbell Triceps Extension"
            },
            {
                "description": "Position barbell overhead with narrow overhand grip. Lower forearm behind upper arm with elbows remaining overhead. Extend forearm overhead. Lower and repeat."
            },
            {
                "equipment": "Barbell"
            },
            {
                "muscles-front": ""
            },
            {
                "muscles-back": ""
            },
            {
                "workout-video-url": "https://www.youtube.com/watch?v=a2TkTaXagRQ"
            }
        ]
    },
    {
        "bench-press-narrow-grip": [
            {
                "exercise": "bench-press-narrow-grip"
            },
            {
                "description": "Lay down on a bench, the bar is directly over your eyes, the knees form a slight angle and the feet are firmly on the ground. Hold the bar with a narrow grip (around 20cm.). Lead the weight slowly down till the arms are parallel to the floor (elbow: right angle), press then the bar up. When bringing the bar down, don't let it down on your nipples as with the regular bench pressing, but somewhat lower."
            },
            {
                "equipment": "Barbell, bench"
            },
            {
                "muscles-front": ""
            },
            {
                "muscles-back": ""
            },
            {
                "workout-video-url": ""
            }

        ]
    }
]
}

0 个答案:

没有答案