0

我正在开发与地址相关的应用程序。

我用这样的硬编码地址测试了我的函数:

var address = "India Gate , Rajpath, New Delhi, Delhi"; 

它工作正常。

但是现在当我使用该功能通过表单从用户那里获取 adderss 并使用address=form.address.value. 然后alert(address)显示正确address(字符串)。但是函数无法对其工作(对于与硬编码相同的地址)。

基本上我使用的是谷歌地图 API。form.name.value那么我应该知道有什么特别之处吗?

geocoder = new google.maps.Geocoder();
var address = form.address.value;
alert(address);
//var address = "India Gate , Rajpath, New Delhi, Delhi";

geocoder.geocode({
    'address': address
}, function(results, status) {

    if(status == google.maps.GeocoderStatus.OK) {
        alert("hi");
        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();
        addpin(latitude, longitude);
    }
});
<form>
    Your Name: <input type="text" name="name"/><br/>
    Your Email:<input type="text" name="email"/><br/>
    Company name:<input type="text" name="company"/><br/>
    Street address:<input type="text" name="address"/><br/>
    <input type="submit" value="submit" onclick="getlatlong(this.form)">
</form>
4

1 回答 1

0

form.name反映到元素的name属性form

<form name="form1" />

console.assert(form.name == "form1");

你可以使用form.elements.name你想要的。但在这种情况下,您无法到达length命名元素,因为那是form.elements集合的长度。

于 2012-07-04T20:19:17.237 回答