我有这个 json 文件:
{
"categories": [
{
"producerName": "Canon",
"productName": [
{
"kamera": "EOS 5D",
"link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
"kamera": "EOS 5D Mark II",
"link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
"kamera": "EOS 5D Mark III",
"link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
"productObj": [
{
"objektiv": "Canon EF 15mm f/2.8 Fisheye",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
},
{
"objektiv": "Canon EF 8-15mm f/4L Fisheye USM",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
},
{
"objektiv": "Canon EF 14mm f/2.8L USM",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
},
{
"objektiv": "Canon 24mm f/1.4 L USM",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
}
]
},
{
"kamera": "EOS 7D",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/",
"productObj": [
{
"objektiv": "Canon EF 15mm f/2.8 Fisheye",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
},
{
"objektiv": "Canon EF 8-15mm f/4L Fisheye USM",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
},
{
"objektiv": "Canon EF 14mm f/2.8L USM",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
},
{
"objektiv": "Canon 24mm f/1.4 L USM",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
}
]
}
]
},
{
"producerName": "Nikon",
"productName": [
{
"kamera": "D800",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/",
"productObj": [
{
"objektiv": "Canon EF 15mm f/2.8 Fisheye",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
}
]
}
]
},
{
"producerName": "Olympus",
"productName": [
{
"kamera": "OM-D EM-5",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/",
"productObj": [
{
"objektiv": "Canon EF 15mm f/2.8 Fisheye",
"link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
}
]
}
]
}
]
}
我通过带有 json 回调的 jQuery 解析这个,效果很好。
唯一的问题是我想将所有带有“link”的“camera”连接到“productObj”,但我总是只得到最后一个字符串。
希望您能够帮助我。
jQuery部分:
$('#model').on("change", function() {
var indx = $("#cat").find(".selectBox-label").attr("id");
//console.log(indx);
var item = '<option id="std">2. Kamera auswählen:</option>';
for (n=0; n < menu[indx].productName.length; n++){
item += '<option id="'+[n]+'">' + menu[indx].productName[n].kamera + '</option>';
}
$('#camera').html(item);
$('#camera').selectBox('refresh');
$('#camera').selectBox('enable');
$.cookie('m', menu[indx].producerName);
$.removeCookie('k');
$.removeCookie('o');
//second onChange
$('#camera').on("change", function() {
var indy = $("#items").find(".selectBox-label").attr("id");
var goRef = menu[indx].productName[indy].link;
if(goRef) {
$.cookie('k', menu[indx].productName[indy].kamera);
if($(".leftfinder").length > 0) {
$(".gofind").css({'background': 'url(leftgofind.png)', 'cursor':'pointer'});
}else {
$(".gofind").css({'background': 'url(gofindbt.png)', 'cursor':'pointer'});
}
$(".gofind").on("click", function() {
window.location.href = "url"+goRef;
});
}
});