开发者

Javascript function only continuing after the 'else'

开发者 https://www.devze.com 2023-02-22 17:00 出处:网络
function getNewMessages(id) { clearJqGrid(); var lat; var lng; var jsonData; var geocoder = new google.maps.Geocoder();
function getNewMessages(id)
        {
            clearJqGrid();
            var lat;
            var lng;
            var jsonData;
            var geocoder = new google.maps.Geocoder();
            var miles = $('#milesAway').val();

            // find what the user is locating by
            if ($('#zipCode').is(':visible'))
            {
                var zipText = $('#zipCode').val();
                if (isValidUSZip(zipText))
                {
                    geocoder.geocode( { 'address': zipText}, function(results, status) {
                      if (status == google.maps.GeocoderStatus.OK) {
                         lat = results[0].geometry.location.lat();
                         lng = results[0].geometry.location.lng();
                         map.setCenter(results[0].geometry.location);
                      } else {
                        alert("Zip Code couldn't be located.");
                      }
                    });
                }
                else
                {
                    alert('Please enter a valid US zip code.');
                }
            }
            else if ($('#cityState').is(':visible'))
            {
                var address = $('#cityState').val();
                geocoder.geocode( { 'address': address}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        lat = results[0].geometry.location.lat();
    开发者_JAVA技巧                    lng = results[0].geometry.location.lng();
                        map.setCenter(results[0].geometry.location);
                    } else {
                    alert("City/State combination couldn't be found.");
                    }
                });
            }
            else
            {
                var latlng = map.getCenter();
                lat = latlng.lat();
                lng = latlng.lng();
                jsonData = 
                {
                    latitude: lat,
                    longitude: lng,
                    milesAway: miles
                }
            }

            $.ajax(
            {
                type: "POST",
                url: "<%= Url.Action("CalcLocation", "Home") %>",
                data: jsonData,
                success: function (result) {
                    var messages = result;
                    for(var i=0;i<messages.length;i++) 
                        jQuery("#responseMessages").jqGrid(
                                                            'addRowData',
                                                            i+1,
                                                            {distance:messages[i].distance,age:messages[i].age,message:messages[i].message}
                                                          );
                },
                error: function (error) {

                }
            });
        }

So it's only making the AJAX call if it goes through the 'else' part of the if statement, not the others. Why?


Milimetric actually got it right, I believe - you're missing the semicolon in your jsonData object literal definition. That combined with JavaScript's auto-semi-colon insertion and the way your braces {} are written to begin on a new line, is very likely the problem. Put in a semi-colon in there and it should work fine.

In other C-like languages, putting the opening brace on a new line or on the same line is a personal preference but in JavaScript, it does make a difference and so I'd recommend you make a conscious effort to put opening braces on the same line.

  • http://encosia.com/2011/03/21/in-javascript-curly-brace-placement-matters-an-example/
  • http://robertnyman.com/2008/10/16/beware-of-javascript-semicolon-insertion/
0

精彩评论

暂无评论...
验证码 换一张
取 消