Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery ajax_Javascript_Jquery_Google Geocoder - Fatal编程技术网

Javascript jQuery ajax

Javascript jQuery ajax,javascript,jquery,google-geocoder,Javascript,Jquery,Google Geocoder,我试图按照这段代码使用谷歌地理代码API,但我做错了什么。我从地理代码开始,然后是jQuery验证器,但不知怎么的,状态没有设置好。有什么帮助吗 jQuery(document).ready(function($) { var shvalidator = $('#post').validate({ rules: { post_title: { required:true } }, messages: { post_title: {re

我试图按照这段代码使用谷歌地理代码API,但我做错了什么。我从地理代码开始,然后是jQuery验证器,但不知怎么的,状态没有设置好。有什么帮助吗

jQuery(document).ready(function($) {

  var shvalidator = $('#post').validate({
    rules: {
      post_title: { required:true }
    },
    messages:  { 
        post_title: {required:jQuery.format("Enter the name of the venue")} 
    }
  });

  function getAddress() {

    var geocoder = new google.maps.Geocoder();

    var fulladdress = $('#sh_venue_address1').val()+' ' \
      +$('#sh_venue_address2').val()+' ' \
      +$('#sh_venue_city').val()+' ' \
      +$('#sh_venue_state').val()+' ' \
      +$('#sh_venue_postalcode').val();

    alert(fulladdress );
    var a,b;

    geocoder.geocode( { 'address': fulladdress},
    function(results, status) {
     //**// 
     if (status == google.maps.GeocoderStatus.OK) {
        a = results[0].geometry.location.lat();
        b = results[0].geometry.location.lng();
        setAddress(a,b);
      }
    });
  }

  // this is the callback method that waits for response from google
  function setAddress(lat,lng) {
    $('#sh_venue_latitude').val(lat);
    $('#sh_venue_longitude').val(lng);
    alert ($('#sh_venue_latitude').val());
  }

  // this is used to validate the form before submitting
  $('#post').submit(function() {
    getAddress();
    if (shvalidator.valid()==true){
      return true;
    } else {
      $('#ajax-loading').hide();
      $('#publish').removeClass('button-primary-disabled');
      return false;
    }
  });
});

我假设它不起作用,因为函数getAddress在处理提交之前没有完全执行(因为它是对地址进行地理编码的异步调用)。 我想在submit函数中执行以下操作:

getAddress(function(){
if (shvalidator.valid()==true){
      return true;
    } else {
      $('#ajax-loading').hide();
      $('#publish').removeClass('button-primary-disabled');
      return false;
    }
});
并将getAddress函数更改为:

function getAddress(callback){
...
setAddress(a,b);
callback();

...
}

do console.log(arguments),您得到了什么?我得到了未定义的状态代码在//**/之前运行良好,但是它跳过了变量a和b的设置,因为状态是未定义的