JavaScript 参数不通过函数传递

javascript argument not passing through a function

本文关键字:函数 参数 JavaScript      更新时间:2023-09-26

我正在尝试获取用户的当前城市以及单击时通过getCity函数传递的任何参数,但是我未定义"x"变量。 这是代码。

<button onclick="getCity('burger')">burger</button>
<button onclick="getCity('steak')">steak</button>
<button onclick="getCity('taco')">taco</button>
<script type="text/javascript">
//get city
function getCity(x) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPos);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
//get lat and long
function showPos(position) {    
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    //get address
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
    'location': latlng 
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    //get city
    if (results[0]) {
        var city = results[0]['address_components'][2].long_name;
        var x = x;
        alert(x +" "+ city); // i'm getting undefined + currentcity
    }
}
});
}
</script>

我正在获得未定义+当前城市。 如果我点击汉堡按钮,我该如何获得汉堡 + 当前城市?

你倒数第二句话

var x = x;

将未定义的 X 分配给 x。两个"x"都是未定义的。

要使脚本正常工作,您必须声明一个全局变量

<button onclick="getCity('burger')">burger</button>
<button onclick="getCity('steak')">steak</button>
<button onclick="getCity('taco')">taco</button>
<script type="text/javascript">
var food;
//get city
function getCity(x) {
    if (navigator.geolocation) {
        food = x;
        navigator.geolocation.getCurrentPosition(showPos);
    } else { 
        //x.innerHTML does not work because x is a string not an element
        this.innerHTML = "Geolocation is not supported by this browser.";
    }
}
//get lat and long
function showPos(position) {    
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    //get address
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
    'location': latlng 
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    //get city
    if (results[0]) {
        var city = results[0]['address_components'][2].long_name;
        var x = food;
        alert(x +" "+ city);
    }
}
});
}
</script>

另一种可能的解决方案是传递它以将x传递给showPos函数。

<button onclick="getCity('burger')">burger</button>
<button onclick="getCity('steak')">steak</button>
<button onclick="getCity('taco')">taco</button>
<script type="text/javascript">
//get city
function getCity(x) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position){
            showPos(position, x); 
        });
    } else { 
        //x.innerHTML does not work because x is a string not an element
        this.innerHTML = "Geolocation is not supported by this browser.";
    }
}
//get lat and long
function showPos(position, x) {    
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    //get address
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
    'location': latlng 
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    //get city
    if (results[0]) {
        var city = results[0]['address_components'][2].long_name;
        //var x = food;     x is already a parameter of this function so don't declare it again
        alert(x +" "+ city);
    }
}
});
}
</script>