获取json兄弟数据 [英] Getting json sibling data
问题描述
我目前有一个json对象,可以遍历并输出链接列表.
I currently have a json object, that I loop through and output a list of links.
请参阅小提琴: http://jsfiddle.net/jasonday/hzZ8j/
根据json中的storeID为每个链接提供一个ID.
each link is given an id based upon the storeID in the json.
我想做的是,当单击链接时,它会在json中找到ID,然后将同级元素"otherData"写入#otherDataDiv
What I want to do, is when a link is clicked it finds the id in the json, and then writes the sibling element "otherData" to #otherDataDiv
我已经处理了遍历xml的问题,但是我不确定如何使用json完成此操作.
I've worked with traversing xml, but I'm not sure how to accomplish this with json.
任何帮助将不胜感激.谢谢.
Any help would be appreciated. thanks.
推荐答案
您只需要像这样循环遍历:
You just have to loop over, like this:
var target = "store17",
foundStore = {};
for(var k1 in object.state){ var state = object.state[k1];
for(var k2 in state.store){ var store = state[k2];
if (store.storeid == target){
foundStore = store;
break;
}
}
}
但是,如果您使用的是jQuery模板,则可以在元素的数据数组中查找"tmplItem".
However, if you were using jQuery templates then you could just look for 'tmplItem' in the data array on the element.
此外,如果您不是为此手动构建HTML,则建议在此项目中使用jQuery数据.它将极大地解决您的问题.
Additionally, if you weren't building the HTML manually for this, I would suggest using jQuery data here for this project. It would solve your problem immensely.
要存储:$(selector).data('unique name here',data);
要检索:var usefulname = $(selector).data('unique name here');
,然后在您的onclick中为每个链接都可以:
and then in your onclick for each link you could:
var otherData = $(this).data('unique name here').otherData;
这篇关于获取json兄弟数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!