简体   繁体   English

javascript参数未通过函数

[英]javascript argument not passing through a function

i'm trying to get a user's current city plus whatever argument that's being passed through getCity function when clicked but i'm getting undefined for 'x' variable. 我正在尝试获取用户的当前城市以及单击时通过getCity函数传递的任何参数,但是我无法获取'x'变量的定义。 here's the code.. 这是代码。

<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>

i'm getting undefined + currentcity. 我越来越不确定+ currentcity。 how do i make it so i get burger + currentcity if i click on burger button? 如果我点击“汉堡”按钮,我如何才能获得汉堡+ currentcity?

You second last statement 您倒数第二句话

var x = x;

asssigns an undefined x to x. 将未定义的x分配给x。 Both "x"s are undefined. 两个“ x”均未定义。

For your script to work you have to declare a global variable 为了使脚本正常工作,您必须声明一个全局变量

<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>

Another possible solution would be passing it to pass the x to the showPos function. 另一个可能的解决方案是传递它以将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>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM