我使用提交前鉤子爲了在提交之前運行一些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