1

我要做的是从导入的邮政编码中获取纬度和经度。只要我只坚持一个输入(第一个或第二个单独),它就可以正常工作。一旦我尝试使用它们,我就无法让它工作。我正在使用 onchange 事件..

请看一下表格:

<form name="latlon" action="test2a.php" method='post' onchange="return usePointFromPostcode(document.getElementById('postcode').value) && usePointFromPostcode1(document.getElementById('postcode1').value) ">
    <input id="postcode" type="text" size="10" /><br>
    <input id="postcode1" type="text" size="10"/><br>
    Latitude:<input name="lat" type="text" id="lat" size="10"/><br />
    Longitude:<input name="lon" type="text" id="lon" size="10"/><br><br>
    Latitude:<input name="lat1" type="text" id="lat1" size="10"/><br />
    Longitude:<input name="lon1" type="text" id="lon1" size="10"/>
    <input type="submit" value="Get Lat / Lon"/>
</form>

在 JavaScript 中,这是我用来获取纬度和经度的:

function usePointFromPostcode(postcode, callbackFunction) {
    localSearch.setSearchCompleteCallback(null,

    function () {

        if (localSearch.results[0]) {
            var resultLat = localSearch.results[0].lat;
            var resultLng = localSearch.results[0].lng;
            var point = new GLatLng(resultLat, resultLng);
            document.forms['latlon'].lat.value = resultLat;
            document.forms['latlon'].lon.value = resultLng;
            callbackFunction(point);
        } else {
            alert("Postcode not found!");
        }
    });

    localSearch.execute(postcode + ", UK");
}

function usePointFromPostcode1(postcode1, callbackFunction) {

    localSearch.setSearchCompleteCallback(null,

    function () {

        if (localSearch.results[0]) {
            var resultLat = localSearch.results[0].lat;
            var resultLng = localSearch.results[0].lng;
            var point = new GLatLng(resultLat, resultLng);
            document.forms['latlon'].lat1.value = resultLat;
            document.forms['latlon'].lon1.value = resultLng;
            callbackFunction(point);
        } else {
            alert("Postcode not found!");
        }
    });

    localSearch.execute(postcode1 + ", UK");
}

我完全被困在这里。我究竟做错了什么?将不胜感激任何帮助,并提前感谢您。

4

2 回答 2

0

有效,但并不完美....

 <input type="text" size="5"  name="postcode1" id="postcode1" maxlength="4" style="width: 32px" onchange="usePointFromPostcode(document.getElementById('postcode1').value)"  >

 <input type="text" size="5"  name="postcode2" id="postcode2" maxlength="4" style="width: 32px" onchange="usePointFromPostcode1(document.getElementById('postcode2').value)">

有什么建议么 ....

于 2013-05-10T15:49:33.180 回答
0

尝试改变

<form name="latlon" action="test2a.php" method='post' onchange="return usePointFromPostcode(document.getElementById('postcode').value) && usePointFromPostcode1(document.getElementById('postcode1').value) ">

<form name="latlon" action="test2a.php" method='post' onchange="usePointFromPostcode(document.getElementById('postcode').value); usePointFromPostcode1(document.getElementById('postcode1').value); ">

我不确定您为什么将其用作 onchange 事件的返回值。据我所知,在使用&&.

编辑: 啊,我现在看到了一些东西:您在第一个函数中设置了对 localSearch 变量的回调,并且在第二个函数中覆盖了它。这就是为什么document.forms['latlon'].lat并且document.forms['latlon'].lon永远不会改变价值。只有lat1lon1会。(两次,可能)

您要么必须创建另一个变量,例如 localSearch,然后将其用于第二个函数。

或者很可能更好;您必须检查回调是来自第一个函数还是来自第二个函数。我不确定这是否可能。

于 2012-11-15T13:33:27.620 回答