我使用提交前钩子为了在提交之前运行一些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);
}
});
}
}
geocoder.geocode是异步的。你必须延迟你的表单提交,直到你得到结果(或通过提交处理程序到codeAddress,这将触发表单提交收到结果时) – jbl