2013-10-09 125 views
0

我使用提交前钩子为了在提交之前运行一些JavaScript。因此,我使用下面的代码:jQuery添加后变量不会发送到服务器

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

功能codeAddress()填满了数据的两个隐藏输入字段。数据实际上被填充(到值属性中)。但在服务器端没有数据到达。这里有什么问题?

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); 
      } 
     }); 
    } 
} 
+1

geocoder.geocode是异步的。你必须延迟你的表单提交,直到你得到结果(或通过提交处理程序到codeAddress,这将触发表单提交收到结果时) – jbl

回答

0

由于拉维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> 
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(); 
      ... 
} 
+0

'jQuery('#setup-form')。submit();'start一个无限循环,因为它会触发'jQuery('#setup-form')。submit(function(e){...}'? – tester

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; 
    } 
    }); 
}); 
+0

似乎'return true'对'geocoder.geocode'返回true,后来遇到错误... – tester

+0

我编辑了我的答案,检查它。 –