Jquery表单提交未被触发

时间:2013-05-17 14:50:07

标签: javascript jquery performance debugging

我一直在尝试设计一个超过一周的javascript(我是JS的新手),它在搜索按钮被点击时对位置进行地理编码,如果成功则提交表单。为了使其稍微复杂一些,如果选择了autosuggest中的一个选项,它甚至会在搜索按钮被点击之前对其进行地理编码。

这一切似乎都有效,除了表格永远不会提交,对于我的生活,我无法弄清楚为什么。如果有人能够发现我出错的地方,我会非常感激,我已经花了这么长时间,现在我真的被卡住了。

链接:http://jsfiddle.net/sR4GR/42/

$(function () {
  var input = $("#loc"),
      lat   = $("#lat"),
      lng   = $("#lng"),
      lastQuery  = null,
      lastResult = null, // new!
      autocomplete;

  function processLocation(query, callback) { // accept a callback argument
    var query = $.trim(input.val()),
        geocoder;

    // if query is empty or the same as last time...
    if( !query || query == lastQuery ) {
      callback(lastResult); // send the same result as before
      return; // and stop here
    }

    lastQuery = query; // store for next time

    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ address: query }, function(results, status) {
      if( status === google.maps.GeocoderStatus.OK ) {
        lat.val(results[0].geometry.location.lat());
        lng.val(results[0].geometry.location.lng());
        lastResult = true; // success!
      } else {
        alert("Sorry - We couldn't find this location. Please try an alternative");
        lastResult = false; // failure!
      }
      callback(lastResult); // send the result back
    });
  }

  autocomplete = new google.maps.places.Autocomplete(input[0], {
    types: ["geocode"],
    componentRestrictions: {
      country: "uk"
    }
  });

  google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

  $('#searchform').on('submit', function (event) {
    var form = this;

    event.preventDefault(); // stop the submission

    processLocation(function (success) {
      if( success ) { // if the geocoding succeeded, submit the form
        form.submit()
      }
    });

  });
}); 

1 个答案:

答案 0 :(得分:2)

你在打电话:

processLocation(function (success) {

但是你的processLocation在第二个参数上有回调:

function processLocation(query, callback)

尝试从processLocation中删除查询参数:

function processLocation(callback)

OR 用空白参数调用它:

processLocation(null, function (success) 
相关问题