Jquery 使用按钮单击生成的ID访问JSON对象
我正在尝试编写一个函数,允许我在单击按钮时动态地将内容附加到模式。该函数获取单击的按钮的ID,然后我希望它使用该ID访问JSON对象,以提取要放入模式的信息。我试图使用indexOf获取感兴趣的JSON对象的索引,但它返回的索引为-1。我被卡住了。请帮忙 这是JSJquery 使用按钮单击生成的ID访问JSON对象,jquery,json,Jquery,Json,我正在尝试编写一个函数,允许我在单击按钮时动态地将内容附加到模式。该函数获取单击的按钮的ID,然后我希望它使用该ID访问JSON对象,以提取要放入模式的信息。我试图使用indexOf获取感兴趣的JSON对象的索引,但它返回的索引为-1。我被卡住了。请帮忙 这是JS $(document).ready(function(){ getIdMoreInfo(); function getIdMoreInfo(){ $(".more-info").on("click",function(
$(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": ""
}
]
}
]
}
那只是一个普通的旧对象。你能改变对象的格式吗?在父对象的数组中有自己的对象中的每个属性将使处理它非常困难。@Andreas我认为JSON只不过是对象的子集,这个特定对象满足所有条件,可以将其命名为JSON。@void不,它不是对象的子集。JSON是对象的字符串表示。不,JSON不是自动与javascript对象相同的,它是字符串的数据格式,看起来像javascript对象,但有其他要求,例如双引号等。