0

当我的文本框值更改时如何更改我的标签值。这是我的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoogleMap.aspx.cs" Inherits="_Default" %>

谷歌地图var map,directionsService,directionsDisplay,geocoder,startLatlng,endLatlng,routeStart,routeEnd,startMarker,endMarker,dragTimer,placeService,airportMarkers = [];

function initialize() {
     var latlng = new google.maps.LatLng(0,0);
     routeStart = document.getElementById('routeStart');
     autocomplete = new google.maps.places.Autocomplete(routeStart);
     routeEnd = document.getElementById('routeEnd');
     autocomplete = new google.maps.places.Autocomplete(routeEnd);
     geocoder = new google.maps.Geocoder();
     directionsService = new google.maps.DirectionsService();
     directionsDisplay = new google.maps.DirectionsRenderer({
         suppressMarkers: true
     });
    var myOptions = {
         zoom: 12,
         center: latlng,
         mapTypeId: google.maps.MapTypeId.ROADMAP,
         mapTypeControl: false
     };
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      directionsDisplay.setMap(map);
      directionsDisplay.setPanel(document.getElementById("directionsPanel"));
      placeService = new google.maps.places.PlacesService(map);

      var form = document.getElementById("routeForm");
      form.addEventListener('submit', function(e) {
         e.preventDefault();
         var start = this.elements["routeStart"].value;
         var end = this.elements["routeEnd"].value;

         if (start.length && end.length) {
             calcRoute(start, end);
         }
     });

      google.maps.event.addListenerOnce(directionsDisplay, 'directions_changed',     function() {
        var directions = this.getDirections();
        var overview_path = directions.routes[0].overview_path;
        var startingPoint = overview_path[0];
        var destination = overview_path[overview_path.length - 1];
        addMarker(startingPoint, 'start');
        addMarker(destination, 'end');
     });
 }

function addMarker(position, type) 
{
    var marker = new google.maps.Marker({
        position: position,
        draggable: true,
        animation: google.maps.Animation.DROP,
        map: map
     });

     marker.type = type;

     google.maps.event.addListener(marker, 'drag', function() {
        var marker = this;
        clearTimeout(dragTimer);
         // only update the location if 250ms has passed since last drag
         dragTimer = setTimeout(function() {
             getLocationName(marker.getPosition(), function(name) {
                if (marker.type === 'start') {
                     routeStart.value = name;
                 } else {
                    routeEnd.value = name;
                }
            });
       }, 250);
    });

    google.maps.event.addListener(marker, 'dragend', function() {
        calcRoute(startMarker.getPosition(), endMarker.getPosition());
    });

     if (type === 'start') {
        startMarker = marker;
     } else {
         endMarker = marker;
    }
}

 function displayAirports() {
     placeService.textSearch({
     location: startMarker.getPosition(),
     query: 'airport near, ' + routeEnd.value,
     radius: '100',
     types: ['airport']
      }, function(airports, status) {
         if (status === google.maps.places.PlacesServiceStatus.OK) {
             for (var a in airports) {
                 airportMarkers.push(new google.maps.Marker({
                    position: airports[a].geometry.location,
                    map: map
                }));
            }
        }
    });
}

  function getLocationName(latlng, callback) {
    geocoder.geocode({
         location: latlng
     }, function(result, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            var i = -1,
                locationName = 'Not Found';

             // find the array index of the last object with the locality type
             for (var c = 0; c < result.length; c++) {
                 for (var t = 0; t < result[c].types.length; t++) {
                    if (result[c].types[t].search('locality') > -1) {
                       i = c;
                    }
                }
            }
            if (i > -1) {
                 locationName = result[i].address_components[0].long_name;
            }
             callback(locationName);
        }
    });
}

 function calcRoute(start, end) {
    var request = {
         origin: start,
         destination: end,
         travelMode: google.maps.DirectionsTravelMode.DRIVING
     };
     directionsService.route(request, function(response, status) {
         if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            displayAirports();
        }
     });
 }
 function GetVal(textBoxValue) {
      document.getElementById('<%=Value1.ClientID %>').innerHTML = textBoxValue;

 }
 function GetVal2(textBoxValue) {
      document.getElementById('<%=Value2.ClientID %>').innerHTML = textBoxValue;
 }
</script>
 </head>
   <body onload="initialize()">
        <form id="routeForm" runat="server">
        Start &nbsp;<asp:TextBox ID="routeStart" runat="server"   onchange="GetVal(this.value)" />&nbsp;<asp:RequiredFieldValidator
            ID="RequiredFieldValidator1" runat="server" ControlToValidate="routeStart" 
            ErrorMessage="Enter origin city" ForeColor="Red">* Enter origin   city</asp:RequiredFieldValidator>
         &nbsp;
 End<asp:TextBox ID="routeEnd" runat="server" onchange="GetVal2(this.value);"  />&nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator3"
        runat="server" ControlToValidate="routeEnd" 
             ErrorMessage="Enter destination city" ForeColor="Red">* Enter destination city</asp:RequiredFieldValidator>
       &nbsp;<asp:Button ID="submit" runat="server" Text="Find Route" />
       <br />
        <asp:Label ID="Value1" runat="server"/>&nbsp;&nbsp;
        <asp:Label ID="Value2" runat="server"/>
        </form>
        <div class="clear">
             <div id="directionsPanel" style="float: right; width: 20%; height: 533px">
             </div>
          </div>
         <div id="map_canvas" style="float: left; width: 80%; height: 700px;">
        </div>
   </body>
   </html>
4

2 回答 2

1

通过简单地连接到文本框的 on textchanged 事件并在后面的代码中分配标签的值,这将使您的生活变得更加轻松。

标记

 <asp:TextBox ID="routeStart" runat="server" OnTextChanged="routeStart_textChanged"    AutoPostBack="True" />

代码背后

VB

 Protected Sub routeStart_textChanged(sender As Object, e As EventArgs)

  Label1.Text="New Value"

  End Sub

C#

  protected void routeStart_textChanged(object sender, EventArgs e)
    {
Label1.Text = "New Value";

    }

如果它是使用 java 脚本的唯一要求,那么我假设在您的页面某处您有这一行

   routeStart.Attributes.Add("OnChange", "GetVal('" + routeStart.Text + "' );");

您不需要标记中的 onchange 属性。尽管这也有效,但它会让 Visual Studio 不高兴。

于 2013-01-11T09:32:39.093 回答
1

onchange当您离开文本框时(例如,当文本框失去焦点时),该事件会触发。因此,您在键入时不会看到更改。

要确保在您键入时调用您的函数,请改用onkeyupevent

<asp:TextBox ID="routeStart" runat="server" onkeyup="GetVal(this.value)" />
于 2013-01-11T09:40:25.060 回答