表单提交上的JQuery AJAX不起作用

表单提交上的JQuery AJAX不起作用,jquery,ajax,Jquery,Ajax,我一辈子都搞不懂为什么这行不通。工作太久了,需要一双新的眼睛 我可以调用警报(“错误:找不到城市。请重试”)和警报(“错误:城市太模糊,请重试”) 但这不提交表格!不知道为什么。提前感谢你的帮助 //why won't this submit the form??? if (codes.length == 1) { $('#city_number').val(codes); return true; } $('#real-estate-search').submit(functio

我一辈子都搞不懂为什么这行不通。工作太久了,需要一双新的眼睛

我可以调用
警报(“错误:找不到城市。请重试”)
警报(“错误:城市太模糊,请重试”)

但这不提交表格!不知道为什么。提前感谢你的帮助

//why won't this submit the form???
if (codes.length == 1) {
  $('#city_number').val(codes); 
  return true;
} 

$('#real-estate-search').submit(function() {
  //users won't always click the drop down, so we need to have a best 
  //guess script which guesses which city the customer wants. 

  //get the radio status 
  radio_selection = $('input[name=search_type]:checked', '#real-estate-search').val();
  if(radio_selection == 'city' && !$('#city_number').val() 
     && $('#search_query').val()) {
    alert("if fired!");
    $.ajax({  
      type: "GET",  
      url: "includes/autocomplete.php",  
      data: "query="+ $('#search_query').val(),  
      success: function(data){  
        alert("ajax success!");
        return_data = jQuery.parseJSON(data);
        codes = return_data.data;
        error = null;
        if (codes.length == 0) {
          alert("Error: City not found. Please try again.");
          return false;
        }
        if (codes.length > 1) {
          alert("Error: City too ambiguous, please try again.");
          return false;
        }
        if (codes.length == 1) {
          $('#city_number').val(codes);
          return true;
        }                          
      }
    }); //end of ajax function
  } else return true;

  return false;           
});

由于AJAX请求是异步进行的,因此在调用发生时submit方法将已经返回false,这意味着返回true将不起任何作用,因为它不再位于
submit()
范围内

您需要做的是让回调函数再次启动表单提交,而不是返回true

if (codes.length == 1) {
   $('#city_number').val(codes);   
   $('#real-estate-search').submit();
}    
并添加一条语句,表示无需第二次验证。

什么类型的
$(“#房地产搜索”)
表单
输入
var canSend = false;
$('#real-estate-search').submit(function() {
  if ( !canSend ) {
  //users won't always click the drop down, so we need to have a best 
  //guess script which guesses which city the customer wants. 

  //get the radio status 
  radio_selection = $('input[name=search_type]:checked', '#real-estate-search').val();
  if(radio_selection == 'city' && !$('#city_number').val() 
     && $('#search_query').val()) {
    alert("if fired!");
    $.ajax({  
      type: "GET",  
      url: "includes/autocomplete.php",  
      data: "query="+ $('#search_query').val(),  
      success: function(data){  
        alert("ajax success!");
        return_data = jQuery.parseJSON(data);
        codes = return_data.data;
        error = null;
        if (codes.length == 0) {
          alert("Error: City not found. Please try again.");
          return false;
        }
        if (codes.length > 1) {
          alert("Error: City too ambiguous, please try again.");
          return false;
        }
        if (codes.length == 1) {
          $('#city_number').val(codes);
          canSend = true;
          $('#real-estate-search');
        }                          
      }
    }); //end of ajax function
  } else return true;
  return false;//return false if the form is not valid

  } else {
    return true;//return true if codes.length == 1
  }
});