Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript参数未通过函数_Javascript - Fatal编程技术网

javascript参数未通过函数

javascript参数未通过函数,javascript,Javascript,我试图获取用户的当前城市加上单击时通过getCity函数传递的任何参数,但是对于“x”变量,我没有定义。这是密码 <button onclick="getCity('burger')">burger</button> <button onclick="getCity('steak')">steak</button> <button onclick="getCity('taco')">taco</button> <sc

我试图获取用户的当前城市加上单击时通过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>
汉堡
牛排
墨西哥玉米薄饼卷
//进城
函数getCity(x){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPos);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
//长和长
函数showPos(位置){
var lat=位置坐标纬度;
var lng=位置坐标经度;
//获取地址
var geocoder=new google.maps.geocoder();
var latlng=新的google.maps.latlng(lat,lng);
地理编码({
“位置”:latlng
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
//进城
如果(结果[0]){
var city=results[0]['address\u components'][2].long\u name;
var x=x;
警报(x+“”+city);//我正在获取未定义的+currentcity
}
}
});
}

我的城市变得不明确了。如果我点击burger按钮,我如何才能得到burger+currentcity?

你的第二个最后陈述

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>

您的
getCity(x)
中的
x
是一个字符串。该
x.innerHTML
可能如何工作?您的
x
是一个局部变量,不在两个函数之间共享。请参阅。。当我调用getCity()时,我以为showPos函数在getCity函数bc中。。showPos()也会被执行。有什么建议我可以让它工作吗?thanksi假设x来自getCity(x),onclick函数getCity('argument');?函数showPos不在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(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>