4

这是我的代码: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?
}
4

5 回答 5

8
if (coded === false && $("#loc").val() != "") {
      processLocation();
}

http://jsfiddle.net/E8sNt/2/

或者 -

if(!coded && $('#loc').val()){
  processLocation();
}
于 2013-05-15T21:54:38.757 回答
4

$("#loc").val() != "" 应该管用。

于 2013-05-15T21:55:14.523 回答
4
if (coded === false && $('#loc').val() ) {
      processLocation();
}
于 2013-05-15T21:55:54.853 回答
4

对不起,如果我误解了,但这是你需要的吗?

  if (coded === false && $("#loc").val().length> 0) {
                processLocation();
        }

雪儿

于 2013-05-15T21:58:25.287 回答
2

使用 Javascript

if (coded === false && document.getElementById("loc").value!= "") {
      processLocation();
}
于 2013-05-15T21:59:38.400 回答