2012-02-09 70 views
1

我不能为了我的生活找出为什么这不起作用。一直工作太久,需要一套新的眼睛。表单提交上的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;   
}); 
+0

'$('#real-estate-search')''form'或'input'的类型是什么 – mgraph 2012-02-09 12:39:40

回答

2

由于AJAX请求异步发生,提交方法将已经在呼叫正在发生,这意味着真正的回报会做什么,因为它是在submit()范围不再返回false。

你需要做的是获得回调,重新启动表单提交而不是返回true。

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

并添加一个语句,这意味着它不会需要第二次验证。

+0

你是男人。 – 2012-02-09 12:53:36

+0

干杯!乐意效劳。 – Jivings 2012-02-09 12:55:42

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