表单提交上的JQuery AJAX无法正常工作

时间:2012-02-09 12:34:00

标签: jquery ajax

我不能为我的生活弄清楚为什么这不起作用。已经工作了太久,需要一双新鲜的眼睛。

我可以调用alert("Error: City not found. Please try again.");alert("Error: City too ambiguous, please try again.");

但这不提交表格!不知道为什么。在此先感谢您的帮助。

//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;           
});

2 个答案:

答案 0 :(得分:2)

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

您需要做的是获取回调以再次启动表单提交而不是返回true。

if (codes.length == 1) {
   $('#city_number').val(codes);   
   $('#real-estate-search').submit();
}    

并添加一个声明,表示不需要第二次验证。

答案 1 :(得分:0)

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
  }
});