javascript参数不通过函数

时间:2016-01-15 03:42:35

标签: javascript

我正在尝试获取用户的当前城市以及点击时通过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>

我得到了undefined + currentcity。如果我点击汉堡按钮,我怎么做到这样我得到汉堡+当前城市?

2 个答案:

答案 0 :(得分:1)

你最后一句话

var x = x;

将未定义的x分配给x。两个“x”都是未定义的。

答案 1 :(得分:0)

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

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