我会尽力解释。
我有一个包含 7 个输入(年龄、国家、城市……)的表单,在 JSON 文件中我有很多人。我需要使用表单中的所有条件搜索该 json 文件。例如西班牙&25 岁。
我在数组中有输入数据,但我无法或不知道如何比较该标准并从 JSON 文件中检索信息。
下面的代码:
<!-- Google Maps -->
<script src="assets/js/jquery.ui.map.full.min.js"></script>
<script src="assets/js/jquery.ui.map.extensions.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('#mapa').gmap({'callback': function() {
var self = this;
parsejson = function(arrayvalues){
$.getJSON( 'mapa.json', function(data) {
$.each(arrayvalues, function(x, val) {
if (val.value !== "") {
console.log(val.value);
$.each( data.markers, function(i, marker) {
console.log('marker ' + marker);
// PROBLEM IS HERE, DON'T KNOW HOW TO SOLVE
});
};
});
// Draw markers in map
// $.each( data.markers, function(i, marker) {
// self.addMarker({
// 'position': new google.maps.LatLng(marker.latitude, marker.longitude),
// 'bounds': false
// }).click(function() {
// self.openInfoWindow({'content': marker.content }, this);
// });
// });
});
};
self.getCurrentPosition(function(position, status) {
if ( status === 'OK' ) {
var clientPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
self.addMarker({'position': clientPosition, 'bounds': false});
self.option('center', clientPosition);
self.option('zoom', 12);
self.addShape('Circle', {
'strokeColor': "#008595",
'strokeOpacity': 0.8,
'strokeWeight': 2,
'fillColor': "#008595",
'fillOpacity': 0.35,
'center': clientPosition,
'radius': 500,
'clickable': false
});
}
else {
console.log("else");
var clientPosition = new google.maps.LatLng(40.463667, -3.74922);
self.option('center', clientPosition);
self.option('zoom', 4);
}
});
}});
});
// Search form actions
$('#searcher-form').submit(function(e) {
e.preventDefault();
var $inputs = $(this).serializeArray();
parsejson($inputs);
});
</script>
<!-- End of Google Maps -->
JSON 文件如下所示:
{"markers":[
{
"fromcountry":"Spain",
"fromcity":"San Cristóbal de la Laguna",
"livecountry":"Spain",
"livecity":"San Cristóbal de la Laguna",
"age":25,
"profession":"Diseñador"
"title":"La Laguna",
"latitude":28.469294910391532,
"longitude":-16.329975128173828,
"content":"Mooola"
},
{
"fromcountry":"Spain",
"fromcity":"Madrid",
"livecountry":"Spain",
"livecity":"Santa Crus de Tenerife",
"age":30,
"profession":"Programador"
"title":"Los Majuelos",
"latitude":28.44038127509586,
"longitude":-16.311674416065216,
"content":"Mooola"
}
]}
表单中的数组给了我这个:
Object {name: "regsearchcountry", value: "whatever"}
Object {name: "regsearchcity", value: "whatever"}
Object {name: "regsearchlivecountry", value: ""}
Object {name: "regsearchlivecity", value: "whatever"}
Object {name: "regsearchagefrom", value: ""}
Object {name: "regsearchageto", value: "whatever"}
Object {name: "regsearchprofession", value: ""}
我需要将表单字段与 JSON 中的某些字段进行比较,而不是所有数据。所以我需要将所有这些表单字段与 JSON 进行比较,如果一个为空,则比较其余的。