2013-10-09 64 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

我編輯了我的答案,檢查它。 –