简体   繁体   English

使用Google Map时无法从表单输入文本框中获取价值吗?

[英]Can't get value from form input textbox when using google map?

I using google map finder from demo ramirezcobos and I config: 我使用演示ramirezcobos的 Google地图查找器进行配置:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var marker=false;
function initialize() {
    var myLatlng = new google.maps.LatLng(10.786599576864381, 106.69340372085571);

    var myOptions = {
        zoom: 16,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById("gmap"), myOptions);

    marker = new google.maps.Marker({
        position: myLatlng, 
        map: map
    });

      google.maps.event.addListener(map, 'center_changed', function() {
        var location = map.getCenter();
        document.getElementById("latlng").value = location.lat()+ ', '+location.lng(); 
        placeMarker(location);
      });

      google.maps.event.addListener(map, 'zoom_changed', function() {
        zoomLevel = map.getZoom();
        document.getElementById("zoom_level").innerHTML = zoomLevel;
      });

      google.maps.event.addListener(marker, 'dblclick', function() {
        zoomLevel = map.getZoom()+1;
        if (zoomLevel == 20) {
            zoomLevel = 10;
        }    
        document.getElementById("zoom_level").innerHTML = zoomLevel; 
        map.setZoom(zoomLevel);
      });
      document.getElementById("latlng").value = 10.786599576864381+ ', '+106.69340372085571;
}

function placeMarker(location) {
    var clickedLocation = new google.maps.LatLng(location);
    marker.setPosition(location);
}

window.onload = function(){initialize();};

</script>
<div id="bd"><div id="gmap"></div></div>
<input type="text" name="latlng" id="latlng" value="" />   

When I using firebug is value not show in input[type="text"], It is null 当我使用萤火虫时,input [type =“ text”]中未显示值,则为null

<input id="latlng" type="text" value="" name="latlng">

How to fix it ? 如何解决?

You are setting the value- property of the element, to set a html- attribute (what is not the same), use setAttribute() : 您正在设置元素的value- 属性 ,以设置html- 属性 (不一样),请使用setAttribute()

document.getElementById("latlng").setAttribute('value',location.lat()+ ', '+location.lng()); 

...and you'll see the changes inside firebug's HTML-Tab. ...,您将看到firebug的HTML-Tab中的更改。

But you also may inspect the properties of an element, select the element in the HTML-Tab and then on the right side you'll find all properties inside the DOM-tab. 但是您也可以检查元素的属性,在HTML-Tab中选择该元素,然后在右侧的DOM-tab中找到所有属性。

However, in this case this should not affect your implementation, as long as you didn't try to read the value-property set via node.value='?' 但是,在这种情况下,只要您不尝试读取via node.value='?'设置的值属性,就不会影响您的实现via node.value='?' by using node.getAttribute('value') (or vice versa) 通过使用node.getAttribute('value') (反之亦然)

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

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