这是我的代码:http: //jsfiddle.net/E8sNt/1/
我想知道以下功能:
if (coded === false) {
processLocation();
}
仅当#loc 输入字段中确实包含某些内容时,我如何才能执行此操作。在 sudo 代码中会有点像这样,但我无法计算出正确的代码:
if (coded === false && #loc.val!=0) {
processLocation();
}
这是我的完整代码:
var coded = false;
geocode();
$.cookie("country", "uk");
// GEOCODE FUNCTION
function geocode() {
var input = $('#loc')[0];
var options = {types: ['geocode']};
var country_code = $.cookie('country');
if (country_code) {
options.componentRestrictions = {
'country': country_code
};
}
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
processLocation();
});
$('#searchform').on('submit', function(e) {
if (coded === false) {
processLocation();
}
return true;
});
$("#loc").bind("change paste keyup", function() {
coded = false;
});
}
function processLocation() {
var geocoder = new google.maps.Geocoder();
var address = $('#loc').val();
geocoder.geocode({
'address': address
},
function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
coded = true;
$('#lat').val(results[0].geometry.location.lat());
$('#lng').val(results[0].geometry.location.lng());
} else {
coded = false;
alert("Sorry - We couldn't find this location. Please try an alternative");
}
});
// coded = true; // Do we need this?
}