0

我在一个网站上工作,我想使用图标来选择旅行模式,我有一个工作功能,但它只适用于选择输入。有没有办法修改它以与按钮一起使用?

提前致谢!

功能:

function calcRoute() {
  var selectedMode = document.getElementById("mode").value;
  var request = {
      origin: thuis,
      destination: kabk,
      // Note that Javascript allows us to access the constant
      // using square brackets and a string value as its
      // "property."
      travelMode: google.maps.TravelMode[selectedMode]
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });

HTML - 选择输入

<select id="mode" onchange="calcRoute();">
    <option value="DRIVING">Driving</option>
    <option value="WALKING">Walking</option>
</select>

HTML - 按钮输入 (?)

<form id="mode">
    <input type="button" onchange="calcRoute();" value="DRIVING">
    <input type="button" onchange="calcRoute();" value="WALKING">
</form>
4

2 回答 2

1

按钮没有onchange事件。改用onclick事件

<form id="mode">
    <input type="button" onclick="calcRoute(this);" value="DRIVING" />
    <input type="button" onclick="calcRoute(this);" value="WALKING" />
</form>

编辑 你必须调整你的calcRoute功能

function calcRoute() {
   var selectedMode = document.getElementById("mode").value;
   //...

function calcRoute(btn) {
    var selectedMode = btn.value;
    // ...
于 2012-05-28T16:56:26.720 回答
0
function calcRoute(ev) {
    var ev = ev || window.event,
        btn = (ev.target || ev.srcElement);
    var selectedMode = btn.value;  //"this" will return the element in the demo
    var request = {
        origin: thuis,


<form id="mode">
            <!--Use onClick-->
    <input type="button" onclick="calcRoute(ev);" value="DRIVING" />
    <input type="button" onclick="calcRoute(ev);" value="WALKING" />
</form>

这应该适合你。 :) 工作演示:http: //jsfiddle.net/DerekL/T7NrF/

于 2012-05-28T16:59:08.450 回答