Jquery-仅当字段不是't为空

Jquery - Only process if field isn't empty

本文关键字:为空 字段 Jquery-      更新时间:2023-09-26

这是我的代码: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?
}
if (coded === false && $("#loc").val() != "") {
      processLocation();
}

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

或者-

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

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

如果我误解了,很抱歉,但这是你需要的吗?

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

作弊

使用Javascript

if (coded === false && document.getElementById("loc").value!= "") {
      processLocation();
}