简体   繁体   English

Javascript document.getElementById未从下拉列表中返回完整值

[英]Javascript document.getElementById not returning full value from dropdown list

My dropdown list is populating correctly from my MySQL DB. 我的下拉列表正在从我的MySQL数据库中正确填充。

When I select a marker of one word such as 'Home' the startname and endname vairables write correctly, the problem I am having is that when I have a value in the dropdown list with spaces only the first word is returned. 当我选择一个单词的标记,例如'Home'时,startname和endname vairables正确写入,我遇到的问题是,当我在下拉列表中有一个带空格的值时,只返回第一个单词。

So if 'Central Park' is the value in the dropdown the variables are set to just 'Central' 因此,如果“中央公园”是下拉列表中的值,则变量将设置为“中心”

Here is the code for the dropdown, the javascript for the calcroute function is below: 这是下拉列表的代码,calcroute函数的javascript如下:

     <div id="control_panel" style="float:right;width:80%;text-align:left;padding-top:20px">
    <div style="margin:20px;border-width:2px;">
    <b>Start:</b>
    <select id="start">
<?php
  //php code to get data from mysql
    $prodQuery4=mysql_query("SELECT * FROM markers");
    while ($t=mysql_fetch_array($prodQuery4)) {
  //foreach/while to iterate through elements
    echo '<option value='.$t['name'].'>'.$t['name'].'</option>';
    }
  //end of foreach/while
?>    </select>
<br>
    <b>End:</b>
    <select id="end">
 <?php
  //php code to get data from mysql
    $prodQuery4=mysql_query("SELECT * FROM markers");
    while ($t=mysql_fetch_array($prodQuery4)) {
  //foreach/while to iterate through elements
    echo '<option value='.$t['name'].'>'.$t['name'].'</option>';
    }
  //end of foreach/while
?>    </select>
    <b>Waypoints:</b> 
    <select multiple id="waypoints">
<?php
  //php code to get data from mysql
    $prodQuery4=mysql_query("SELECT * FROM markers");
    while ($t=mysql_fetch_array($prodQuery4)) {
  //foreach/while to iterate through elements
    echo '<option value='.$t['name'].'>'.$t['name'].'</option>';
    }
  //end of foreach/while
?>    </select>
      <input type="submit" onclick="calcRoute();">
    </div>

Javascript function: Javascript功能:

function calcRoute() {
        var startname = document.getElementById('start').value;
        console.log(startname)
        var endname = document.getElementById('end').value;
        var waypts = [];
        var checkboxArray = document.getElementById('waypoints');
        for (var i = 0; i < checkboxArray.length; i++) {
          if (checkboxArray.options[i].selected == true) {
              $.ajax({
                    url:'phpsearch2.php', 
                    dataType:'json', 
                    data:{name:checkboxArray[i].value},
                    async:false,
                    success:function (result)
                    {
    //              console.log(result)
                    waypoint = new google.maps.LatLng(result[0].lat, result[0].             lng);
            }});
            waypts.push({
                location:waypoint,
                stopover:true});
          }
        }

$.ajax({
    url:'phpsearch2.php', 
    dataType:'json', 
    data:{name:startname},
    async:false,
    success:function (result)
{
//  console.log(result)
    origin = new google.maps.LatLng(result[0].lat, result[0].lng);
}});

$.ajax({
    url:'phpsearch2.php', 
    dataType:'json', 
    data:{name:endname},
    async:false,
    success:function (result)
{
//  console.log(result)
    end = new google.maps.LatLng(result[0].lat, result[0].lng);
}});

    var request = {
            origin: origin,
            destination: end,
            waypoints: waypts,
            optimizeWaypoints: true,
            travelMode: google.maps.DirectionsTravelMode.WALKING
        };

            directionsService.route(request, function(response, status) {
        document.write('<b>'+ origin +'</b>');
          if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            var route = response.routes[0];
            var summaryPanel = document.getElementById('directions_panel');
            summaryPanel.innerHTML = '';
            // For each route, display summary information.
            for (var i = 0; i < route.legs.length; i++) {
              var routeSegment = i + 1;
              summaryPanel.innerHTML += '<b>Time for a Walkabout </b><br>';
              summaryPanel.innerHTML += '<b>From ' + startname + '   </b>';
              summaryPanel.innerHTML += '<b>to ' + endname + '('+ route.legs[i].distance.text +')</b><br>';
//            summaryPanel.innerHtml += '<b>' + waypts.toString() + '   </b>';
          } 
          }
        });
     }
//}

You are not quoting your option value attributes so a space means the end of the attribute value 您没有引用选项值属性,因此空格表示属性值的结尾

echo '<option value="'.htmlspecialchars($t['name']).'">'.htmlspecialchars($t['name']).'</option>';
                  //^                                ^ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM