我正在尝试将HTML表单中的值(这些值来自单选按钮输入)传递给我的Google Maps API脚本。我可以console.log travelMode,它看起来正确并且应该可以正常工作,但是我不断收到此错误:“ Uncaught InvalidValueError:在属性travelMode中:google.maps.TravelMode.DRIVING”。如果我取出变量并直接输入驱动模式,则可以正常工作。
是什么赋予了?这是我的代码,如果需要,我可以提供更多:
// Get the travel method
if (document.getElementById('bicycle').checked) {
var method = "google.maps.TravelMode.BICYCLING";
} else if (document.getElementById('motorcycle').checked) {
var method = "google.maps.TravelMode.DRIVING";
} else {
var method = "google.maps.TravelMode.DRIVING";
};
console.log(method);
var request = {
origin: start,
destination: end,
travelMode: method,
};
最佳答案
删除引号
if (document.getElementById('bicycle').checked) {
var method = google.maps.TravelMode.BICYCLING;
} else if (document.getElementById('motorcycle').checked) {
var method = google.maps.TravelMode.DRIVING;
} else {
var method = google.maps.TravelMode.DRIVING;
};