0

I use a before submit hook in order to run some javascript before the submit. Therefore I use the following code:

jQuery(function() {
    jQuery('#setup-form').submit(function(e) {
        codeAddress();
        return true;
    });
});

The function codeAddress() fills two hidden input fields with data. The data is actually filled (into the value attribute). But on the server's side there is no data arriving. What is the problem here?

FYI:

function codeAddress() {
    var address = document.getElementById('address').value;
    if(address){
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var location = results[0].geometry.location;
                var location2 = '' + location;
                location2 = location2.slice(1, location2.length - 1).split(', ');
                jQuery('#geo-lat').attr('value', location2[0]);
                jQuery('#geo-long').attr('value', location2[1]);

                map.setCenter(location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: location
                });
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
}
4

3 回答 3

1

我认为问题是在隐藏字段中设置值之前提交了表单。您可以做的是将您的功能代码放在提交块中,并在设置隐藏字段后返回 true,以便在提交之前设置您的隐藏字段。

尝试这个

jQuery(function() {
jQuery('#setup-form').submit(function(e) {
    var address = document.getElementById('address').value;
    var isHiddenFieldSet=0;//0 indicates hidden field value is not set
    if(address){
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var location = results[0].geometry.location;
                var location2 = '' + location;
                location2 = location2.slice(1, location2.length - 1).split(', ');
                jQuery('#geo-lat').attr('value', location2[0]);
                jQuery('#geo-long').attr('value', location2[1]);

                map.setCenter(location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: location
                });
                isHiddenFieldSet=1;//Set value to 1 to indicate hidden field value is set
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    if(isHiddenFieldSet){//Submit the form only if hidden field value is set
          return true;
    } else {
         return false;
    }
    });
 });
于 2013-10-09T19:45:23.980 回答
0

由于 Ravi Thakkar 和 mry 的答案不起作用(我评论了原因),我建议我自己的解决方案,但它是基于您的想法:

function codeAddress(submit) {
    var address = document.getElementById('address').value;
    if(address){
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var location = results[0].geometry.location;
                var location2 = '' + location;
                location2 = location2.slice(1, location2.length - 1).split(', ');
                jQuery('#geo-lat').attr('value', location2[0]);
                jQuery('#geo-long').attr('value', location2[1]);

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

                if(submit) jQuery('#setup-form').submit();
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    } else {
        if(submit) jQuery('#setup-form').submit();
    }
}

因此有必要将html中的提交按钮更改为:

<button type="button" class="btn btn-success btn-lg" onclick="codeAddress(true)">Submit</button>
于 2013-10-10T20:19:06.170 回答
0

首先调用您的 codeAddress 函数,然后在您设置值后从该函数内部调用您的提交。像这样

function codeAddress() {
    var address = document.getElementById('address').value;
    if(address){
                .
                .
                .
                jQuery('#geo-lat').attr('value', location2[0]);
                jQuery('#geo-long').attr('value', location2[1]);
                jQuery('#setup-form').submit();
             ...
}
于 2013-10-09T19:35:47.780 回答