简体   繁体   English

jQuery验证成功后的Ajax调用

[英]Ajax call after successfull jQuery validation issue

Using RSV jQuery Validation from here . 从这里开始使用RSV jQuery验证。

Really stuck at one place. 真的卡在一个地方。 I wrote custom error handler for this plug-in (read documentation, it's allowed), now it shows errors, and focuses at exact fields one by one, then at the end of validation my custom handler return (errorInfo.length == 0) ? true : false; 我为此插件编写了自定义错误处理程序(阅读文档,允许使用),现在它显示错误,并一一关注于确切的字段,然后在验证结束时return (errorInfo.length == 0) ? true : false;我的自定义处理程序return (errorInfo.length == 0) ? true : false; return (errorInfo.length == 0) ? true : false; returns true if there is no error. 如果没有错误,则返回true。 The problem is, after this rsv directly send form data to PHP. 问题是,此rsv直接将表单数据发送到PHP之后。 But I want to fire Ajax function after successfull validaton. 但是我想在成功验证后触发Ajax函数。 I wrote another function wellDone() for "on complete" event seems plugin doesn't fire oncomplete function at all. 我为“ on complete”事件写了另一个函数wellDone()似乎插件根本不触发oncomplete函数。 Please help me to fix that problem. 请帮助我解决该问题。

$(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>...
]

If you use a customErrorHandler , then in fact the onCompleteHandler will never be called. 如果使用customErrorHandler ,那么实际上将永远不会调用onCompleteHandler It is assumed you call it yourself at the end of your customErrorHandler when errorInfo.length == 0 . 假设当errorInfo.length == 0时,您可以在customErrorHandler的末尾自己调用它。

可能您应该将从customErrorHandler函数返回的true或false值缓存在一个变量中(所有执行ajax调用的函数都可以使用该变量),并使用它来确定是否触发ajax调用。

Your customErrorHanlder should always return "false", but not " balabala ? true : false" 您的customErrorHanlder应该始终返回“ false”,而不应该返回“ balabala?true:false”

This is for people that are comfortable with javascript and who need a little more control over how the errors are presented. 这适用于熟悉javascript并需要更多控制错误呈现方式的人。 It lets you define your own custom function that gets passed the list of errors that occur on a form submit. 它使您可以定义自己的自定义函数,该函数将传递表单提交时发生的错误的列表。 Here's a simple example that alerts each error in turn. 这是一个简单的示例,可以依次警告每个错误。 Note: the two function parameters MUST be set in order to be passed the error information properly. 注意:必须设置两个功能参数,以便正确传递错误信息。

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**   
}

see the last line of code and its comment? 看到代码的最后一行及其注释? ALWAYS return false :-) 总是返回false :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM