2011-11-20 56 views
0

使用RSV jQuery驗證from here成功jQuery驗證問題後的Ajax調用

真的停留在一個地方。我爲這個插件編寫了自定義錯誤處理程序(閱讀文檔,它是允許的),現在它顯示錯誤,並且一個接一個地關注確切的字段,然後在驗證結束時,如果沒有錯誤,我的自定義處理程序return (errorInfo.length == 0) ? true : false;返回true。問題是,在這個rsv之後直接發送表單數據到PHP。但是我想在成功驗證後觸發Ajax函數。我爲「完成」事件寫了另一個函數wellDone(),看起來插件根本不會觸發oncomplete函數。請幫我解決這個問題。

$(document).ready(function() { 
    $("#signup_form").RSV({ 
     onCompleteHandler: wellDone, 
     customErrorHandler: errorHandler, 
     rules: signup_rules 
    }); 
}); 
function errorHandler(f, errorInfo) 
{ 
    for (var i=0; i<errorInfo.length; i++) 
    { 
     // errorInfo[i][0] contains the form field node that just failed the validation, e.g. 
     errorInfo[i][0].focus(); 
     // errorInfo[i][1] contains the error string to display for this failed field, e.g. 
     $.notifyBar({ 
      cls: "error", 
      html: errorInfo[i][1] 
     }); 

    } 

return (errorInfo.length == 0) ? true : false; 
} 

function wellDone(){ 
    signUp(); 
} 

var signup_rules = [ 
<some validation rules>... 
] 

回答

0

如果使用customErrorHandler,那麼實際上onCompleteHandler將永遠不會被調用。假設你在customErrorHandler結束時自己致電errorInfo.length == 0

+0

也嘗試過。沒有成功。我們可以繼續對Skype進行討論嗎?我真的需要幫助隊友 – heron

0

可能您應該緩存您從customErrorHandler函數中返回的true或false值(它可用於執行ajax調用的所有函數),並使用它來確定是否觸發ajax調用或不。

0

你customErrorHanlder應該總是返回「假」,而不是「BALABALA真的?假的」

這是人們都熟悉javascript和誰需要在錯誤怎麼都是 多一點控制呈現。它讓你 定義你自己的自定義函數,通過表單提交發生的錯誤列表 。下面是一個簡單的例子,依次提示每個 錯誤。注意:必須按 的順序設置兩個功能參數才能正確傳遞錯誤信息。

customErrorHandler: 

$(document).ready(function() { 
    $("#demo_form1").RSV({ 
    customErrorHandler: myReturnFunction, 
    rules: [ 
     // ... 
    ] 
    }); }); 
/**  
    * @param f the form node  
    * @param errorInfo an array of arrays. Each sub-array has two elements: the field node and the error message.  
    */ 
function myReturnFunction(f, errorInfo) { 
    for (var i=0; i<errorInfo.length; i++) 
    { 
    // errorInfo[i][0] contains the form field node that just failed the validation, e.g. 
    errorInfo[i][0].focus(); 
    errorInfo[i][0].style.color = "red"; 

    // errorInfo[i][1] contains the error string to display for this failed field, e.g. 
    alert(errorInfo[i][1]); 
    } 

    return false; // always return false! Otherwise the form will be submitted** 
} 

看到的代碼和註釋的最後一行?總是返回false :-)