2012-02-09 14 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」)の形式または入力の種類は – mgraph

答えて

2

AJAXリクエストが非同期で行われるため、提出方法は、呼が、それはsubmit()スコープにもはやだからtrueが返されて、何もしないだろうという意味、場所を取っていた一方で、既にfalseが返されています。

あなたがする必要があるのは、trueを返すのではなくフォーム送信を再度開始するコールバックを取得することです。

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

また、2回目の検証が必要ないことを意味する文を追加します。

+0

あなたは男です。 –

+0

乾杯!喜んで助けてください。 – Jivings

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 
    } 
}); 
関連する問題