如何在一个文档中使用两个id.getElementById类型函数

How to use two id's in an document.getElementById type function.

本文关键字:id 两个 getElementById 函数 类型 文档 一个      更新时间:2023-09-26

试图使用两个插入文本框id来形成谷歌地图的地址。我可以使用一个Id,但是县城没有影响。

<div id="map" style="width: 400px; height: 300px;"></div> 
</br>
<input type="text" id="search"> Street
</br>
<input type="text" id="search2"> County
</br>
<input type="button" onclick="geocode();" value="Go">
function geocode () {
    geocoder.geocode({
       'address': document.getElementById('search','search2').value
    }, 

getElementById只返回一个元素,所以你不能用它一次访问两个元素。

一次访问一个元素,我猜您希望将这些值放在一起,并在它们之间使用分隔符,如空格:

'address': document.getElementById('search').value + ' ' + document.getElementById('search2').value

传递两个id是不可能的。您需要这样的内容:

'address': document.getElementById('search').value + ', ' + document.getElementById('search').value