简体   繁体   中英

How can I get a javascript variable text value in HTML lable tag

I am trying to get a location coordinates by onchange event in javascript and I want to display a that coordinates in a label. I have tried following scenario I can get that coordinates by using alert box But I could not display on a label by using document.getElementsByName("coordinates").innerHTML = text3;

I have share my code below

 <%-- Created by IntelliJ IDEA. User: rajee Date: 2/15/15 Time: 1:19 PM To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Create new Survey</title> </head> <body> <h2>simple survey form</h2> <form name="surveyform" method="post" action="CreateSurvey"> <table> <tr><td>Family name:</td><td><input type="text" name="familyName"></td></tr> <tr><td>First name:</td><td><input type="text" name="firstName"></td></tr> <tr><td>Middle name:</td><td><input type="text" name="middleName"></td></tr> <tr><td>Gender:</td><td><input type="radio" name="sex" value="male" checked>Male</td><td><input type="radio" name="sex" value="female">Female</td></tr> <tr><td>Birthday:</td><td><input type="date" name="dob"></td></tr> <tr><td>Income : A?B?C?D?</td><td><input type="text" name="income"></td></tr> <tr><td>Complete Address:</td><td><input type="text" name="address" id="address" onchange="codeAddress();"></td></tr> <tr><td>Coordinates:</td><td><label id="coordinates" name="coordinates"></label></td></tr> <tr><td>Mobile number:</td><td><input type="tel" name="mobileno"></td></tr> <tr><td>Email Address:</td><td><input type="email" name="email"></td></tr> <tr><td>Present internet provider:</td><td><input type="text" name="iprovider"></td></tr> <tr><td>Positive comments with present provider:</td><td><textarea name="comments" cols="40" rows="5" ></textarea></td></tr> <tr><td>Negative remarks with present provider:</td><td><textarea name="remarks" cols="40" rows="5" ></textarea></td></tr> <tr><td><input type="submit" name="addsurvey" value="save survey details"></td><td><input type="reset" name="cancel" value="cancel"></td></tr> </table> </form> </body> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script> <script runat=server> var geocoder; var map; function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(-34.397, 150.644); var mapOptions = { zoom: 8, center: latlng } map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); } function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { //alert(results[0].geometry.location); var text3 = results[0].geometry.location; alert(text3); document.getElementsByName("coordinates").innerHTML = text3; map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } else { alert('Geocode was not successful for the following reason: ' + status); } }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </html> 

document.getElementsByName() returns a collection of all elements that match the specified name and the collection index starts at 0 .

So if the label is first label in DOM it would be

  document.getElementsByName('coordinates')[0].innerHTML = "text"

尝试使用ID它会工作

 document.getElementsById('coordinates').innerHTML = "text"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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