对象没有方法'indexOf'

Eri*_*rik 3 javascript indexof

我有以下功能(取自Elevation Service @ Google Maps API),例如63.00425720214844当我点击我使用Google Maps JavaScript API v3创建的地图上的某处时输出:

function getElevation(event) {
    var locations = [];
    var clickedLocation = event.latLng;
    locations.push(clickedLocation);

    var positionalRequest = {
        'locations': locations
    }

    elevator.getElevationForLocations(positionalRequest, function(results, status) {
        if(status == google.maps.ElevationStatus.OK) {
            var s = results[0].elevation
            if(results[0]) {
                alert(s.substring(0, s.indexOf('.') - 1));
            } else {
                alert('Inget resultat hittades');
            }
        } else {
            alert('Det gick inte att hitta höjdskillnaden på grund av följande: ' + status);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我想删除点后面的所有内容,包括点,例如.00425720214844从中删除63.00425720214844但是当我点击地图上的某个地方时,我在控制台中收到此错误消息:Uncaught TypeError: Object 63.00425720214844 has no method 'indexOf'.

我做错了什么?

提前致谢.

Guf*_*ffa 7

该变量s不包含字符串.

您可以使用以下方法将其转换为字符串:

s = s.toString();
Run Code Online (Sandbox Code Playgroud)

如果是数字,您只需使用数字函数:

alert(Math.floor(s));
Run Code Online (Sandbox Code Playgroud)


Mar*_*arc 5

只需用javascript parseInt(63.00425720214844)即可获得63.