最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery - Only process if field isn't empty - Stack Overflow

programmeradmin3浏览0评论

This is my code: /

I was wondering for the following function:

        if (coded === false) {
                processLocation();
        }

How I can get this to execute ONLY if the #loc input field actually has something in it. In sudo code it would be a bit like this, but I can't work out the proper code:

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

This is my full code:

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) {
                optionsponentRestrictions = {
                        'country': country_code
                };
        }

        var autoplete = new google.maps.places.Autoplete(input, options);

        google.maps.event.addListener(autoplete, '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?
}

This is my code: http://jsfiddle/E8sNt/1/

I was wondering for the following function:

        if (coded === false) {
                processLocation();
        }

How I can get this to execute ONLY if the #loc input field actually has something in it. In sudo code it would be a bit like this, but I can't work out the proper code:

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

This is my full code:

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.ponentRestrictions = {
                        'country': country_code
                };
        }

        var autoplete = new google.maps.places.Autoplete(input, options);

        google.maps.event.addListener(autoplete, '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?
}
Share Improve this question asked May 15, 2013 at 21:50 JimmyJimmy 12.5k29 gold badges114 silver badges205 bronze badges 1
  • 2 Upvoted for providing a JSFiddle link, thank you! – Russ Clarke Commented May 15, 2013 at 21:52
Add a ment  | 

5 Answers 5

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

http://jsfiddle/E8sNt/2/

Or -

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

$("#loc").val() != "" should work.

sorry if I misunderstood but is this what you need ?

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

Chears

With Javascript

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

评论列表(0)

  1. 暂无评论