2013-04-20 78 views
0

我想讓我的頭繞着這個函數。然而,每次我運行它時,它都會告訴用戶代碼是不正確的,儘管我確實是這樣。有人建議我在這裏缺陷jquery驗證器代碼始終返回false

HTML是簡單

<form id="Join_league" method="post" action="<? echo $HTTP_SERVER_VARS['PHP_SELF'];?>"> 
    <input name="league_code" id="league_code" class="input"> 
        <br><br> 
    <input id="Join_button" name="Join_button" value="Enter League" type="submit"/> 
</form> 

這是我的JavaScript/AJAX功能

  <script src="http://code.jquery.com/jquery-latest.js"></script> 
      <script type="text/javascript" src="http://jzaefferer.github.com/jquery validation/jquery.validate.js"></script> 


    <script type="text/javascript"> 

     $(document).ready(function() { 
$.validator.addMethod("valid_code",function(value,element){ 
    var parameter=""; 
    $.ajax({ 
      url: "check_league_code.php", 
      type: "POST", 
      async: false, 
      data: parameter, 

      success:function(output) 
       { 
        return true; 
       } 
    }); 
}); 


$("#Join_league").validate({ 
    rules: { 
    league_code: {number : true,required : true , 
           valid_code: true 
              } 
}, 
messages: { 
    league_code: { required:"<strong>Please enter code</strong>", 
        number:"<strong>Please enter number only</strong>", 
       valid_code:"<strong>Sorry incorrect code entered</strong>" 
     } 

} 

    }) 

}); 

</script> 

有什麼建議?

+0

http://jsbin.com/urebug/4/watch – 2013-04-20 21:58:43

+0

它總是'FALSE'因爲你永遠不要碰到你的'成功'回調,因爲你的'ajax'代碼中顯然有什麼問題。否則,你的jQuery驗證代碼工作正常:http://jsfiddle.net/vvqFB/1/ – Sparky 2013-04-21 15:27:39

+0

我有它更新,並寫入到成功函數的控制檯無論從PHP文件傳回來。但是,當我硬編碼回來時,它不能提供真正的價值。我需要別的什麼,更新代碼http://jsbin.com/urebug/4/edit – 2013-04-21 16:02:19

回答

0

這是我用來返回true代碼或假

<script type="text/javascript"> 
$(document).ready(function() { 

    jQuery.validator.addMethod("valid_code", function(value,element) { 
      var isSuccess = false; 
      $.ajax({ url: "check_league_code.php", 
      data: "", 
      async: false, 
      success: function(msg) { 
         if(msg === "1") 
           { 
           isSuccess=true; 
           } 
        } 
      }); 
       return isSuccess; 
      },"<strong>Sorry incorrect code entered</strong>"); 



    $("#Join_league").validate({ 
     rules: { 
      league_code: { 
       required : true, 
       number : true, 
       valid_code: true 
         }, 
      messages: { 
       league_code: { 
        required: "<strong>Please enter code</strong>", 
        number: "<strong>Please enter number only</strong>", 
        valid_code: "<strong>Sorry incorrect code entered</strong>" 
          } 
        } 
     } 
    }); 

});