从异步javascript函数返回值

时间:2015-06-16 21:47:19

标签: javascript forms asynchronous

我无法想象我的生活。

我有一个在提交时运行一些javascript的表单。

<form name="myform" action="response.php" method="POST" onsubmit="return codeAddress(returnValue)">

但是,js是异步的,表单在代码完全执行之前提交。该值或该值根本不会返回。

这是js:

function codeAddress(callback) {

    geocoder = new google.maps.Geocoder();
    address = document.getElementById('address').value;
    fullAddress = address.concat(" ,BC, Canada")
    geocoder.geocode({
        'address': fullAddress
    }, function (results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            lat = results[0].geometry.location.A;
            lng = results[0].geometry.location.F;

            SFULatLng = new google.maps.LatLng(49.2799367, -122.91995580000003);
            userAddress = new google.maps.LatLng(lat, lng);
            distance = google.maps.geometry.spherical.computeDistanceBetween(SFULatLng, userAddress);

            return callback();

        } else {
            alert('That address could not be found.' + status);
        }
    });
}

function returnValue() {
    return false;
}

为什么这不会返回false到提交按钮并暂停表单?

0 个答案:

没有答案