簡體   English   中英

jQuery驗證成功后的Ajax調用

[英]Ajax call after successfull jQuery validation issue

從這里開始使用RSV jQuery驗證。

真的卡在一個地方。 我為此插件編寫了自定義錯誤處理程序(閱讀文檔,允許使用),現在它顯示錯誤,並一一關注於確切的字段,然后在驗證結束時return (errorInfo.length == 0) ? true : false;我的自定義處理程序return (errorInfo.length == 0) ? true : false; return (errorInfo.length == 0) ? true : false; 如果沒有錯誤,則返回true。 問題是,此rsv直接將表單數據發送到PHP之后。 但是我想在成功驗證后觸發Ajax函數。 我為“ on complete”事件寫了另一個函數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>...
]

如果使用customErrorHandler ,那么實際上將永遠不會調用onCompleteHandler 假設當errorInfo.length == 0時,您可以在customErrorHandler的末尾自己調用它。

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

您的customErrorHanlder應該始終返回“ false”,而不應該返回“ balabala?true:false”

這適用於熟悉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 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM