简体   繁体   English

如何将值传递给Javascript中的另一个函数?

[英]How to pass value to another function in Javascript?

I am trying to get Google Maps address_components and have only managed to get do the following: 我正在尝试获取Google Maps的address_components ,并且仅设法执行以下操作:

  $(function() {
    $("#spot_address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term }, function(results, status) {
          for (var i = 0; i < results[0].address_components.length; i++)
          {
              var addr = results[0].address_components[i];
              if (addr.types[0] == 'country') 
                  country: addr.long_name;
          }
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng()
            }
          }));
        })
      },

  //This bit is executed upon selection of an address

  select: function(event, ui) {
    alert(ui.country);
    $("#spot_lat").val(ui.item.latitude);
    $("#spot_lng").val(ui.item.longitude);
    var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
    marker.setPosition(location);
    map.setCenter(location);
  }
});

}); });

Over here I am trying to test the alert(ui.country) when the item is selected from the autocomplete list. 在这里,我正在尝试从自动完成列表中选择alert(ui.country)时测试alert(ui.country) However, I am only getting undefined instead. 但是,我只是变得undefined It's not passed. 没有通过 What have I done wrong? 我做错了什么?

Thanks. 谢谢。

Check if property ui.country exists FireBug or Chrome Inspect Scripts 检查属性ui.country是否存在FireBug或Chrome检查脚本

And If property country exist in item then just: 如果项目中存在财产国家,则只需:

response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng(),
              country: item.country, // Or other location of this property
            }
          }));

Add to 添加

      response($.map(results, function(item) {
        return {
          country: item.country

Finally got it done with a help of a friend. 终于在朋友的帮助下完成了。

Here's the full code: 这是完整的代码:

$(function() {
    $("#address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {
        geocoder.geocode( {'address': request.term }, function(results, status) {
      for (var i = 0; i < results[0].address_components.length; i++)
              {
                  var addr = results[0].address_components[i],
                      country1;
                  if (addr.types[0] == 'country') 
                      country1= addr.long_name;
              }
          response($.map(results, function(item) {
            return {
              label:  item.formatted_address,
              value: item.formatted_address,
              latitude: item.geometry.location.lat(),
              longitude: item.geometry.location.lng(),
              country: country1
            }
          }));
        })
      },
      //This bit is executed upon selection of an address
      select: function(event, ui) {
        alert(ui.item.country);
        $("#latitude").val(ui.item.latitude);
        $("#longitude").val(ui.item.longitude);
        var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
        marker.setPosition(location);
        map.setCenter(location);
      }
    });
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 JavaScript 变量值传递给另一个 JavaScript 函数? - how to pass JavaScript variable value to another JavaScript function? JavaScript:如何将对象值从一个函数传递给另一个函数 - JavaScript: how to pass object value from one function to another 如何将值从一个 javascript 函数传递到另一个 - How to pass the value from one javascript function to another 当我在函数中传递值时,如何将javascript上的值传递给另一个javascript文件? - How to pass value from on javascript to another javascript file when I pass value in function? 自动将检索到的值从javascript函数传递给另一个javascript函数? - Automatically pass the retrieved value from a javascript function to another javascript function? 如何使用Javascript从下拉列表中选择一个值并将该值作为var传递给另一函数? - How does one use Javascript to select a value from a drop down list and pass that value as a var to another function? 在JavaScript中,如何将值从未命名函数传递给另一个未命名函数? - In JavaScript, how can I pass value from unnamed function to another unnamed function? 如何将数组传递给Javascript中的另一个函数? - How to pass an array to another function in Javascript? 如何将JavaScript对象传递给另一个函数? - How to pass JavaScript object to another function? 如何将对象值传递到另一个对象JavaScript - How to pass an object value into another object JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM