2011-11-20 53 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 :-)