Ajax调用成功后jQuery验证问题

Ajax call after successfull jQuery validation issue

本文关键字:验证 问题 jQuery 调用 成功 Ajax      更新时间:2023-09-26

从这里使用RSV jQuery验证。

真的卡在一个地方了。我为这个插件编写了自定义错误处理程序(阅读文档,这是允许的),现在它显示错误,并逐个关注确切的字段,然后在验证结束时,如果没有错误,我的自定义处理程序return (errorInfo.length == 0) ? true : false;返回true。问题是,在此rsv直接发送表单数据到PHP。但是我想在成功验证后触发Ajax函数。我为"oncomplete"事件写了另一个函数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调用。

你的customerrorhandler应该总是返回"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:-)