您好我有以下代码,可以从select中选择一个选项来解析json文件及其参数。我在这里作为示例报告的代码在2个单个文件上执行循环。
由于文件夹中可能有不同的json文件,如何创建一个以编程方式传递该文件夹中存在的所有文件的函数?我正在寻找一些说“直到你找到任何json文件,然后开始解析每个文件”。
$(document).ready(function() {
$("select.species").change(function() {
var selectedSpecies = $(".species option:selected").val();
if (selectedSpecies) {
//geojson request
$.getJSON('resources/prova5.geojson', function(data) {
$.each(data.features, function(key, val) {
$.each(val.properties, function(i, j) { //i = properties l = properties value
if (i == 'Species') {
j = j.replace(/\s+/g, '');
species = j.split(",");
}
}); //first loop ends
}); //second loop ends
species.forEach(function(animal) {
if (animal == selectedSpecies) {
//if the animal is found set the layer to visible
prova1.setVisible(true);
}
});
});
//ends geojson request
//geojson request
$.getJSON('resources/prova7.geojson', function(data) {
$.each(data.features, function(key, val) {
$.each(val.properties, function(i, j) { //i = properties l = properties value
if (i == 'Species') {
j = j.replace(/\s+/g, '');
species = j.split(",");
}
}); //first loop ends
}); //second loop ends
species.forEach(function(animal) {
if (animal == selectedSpecies) {
//if the animal is found set the layer to visible
prova2.setVisible(true);
}
});
});
//ends geojson request
}
});
//clears layers
$("#clearSpecies").click(function() {
prova1.setVisible(false);
prova2.setVisible(false);
});
});