【问题标题】:Ajax call after successfull jQuery validation issuejQuery 验证成功后的 Ajax 调用问题
【发布时间】:2012-01-02 07:47:57
【问题描述】:

使用 RSV jQuery 验证 from here

真的卡在一个地方。我为这个插件编写了自定义错误处理程序(阅读文档,这是允许的),现在它显示错误,并一一关注确切的字段,然后在验证结束时,我的自定义处理程序 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>...
]

【问题讨论】:

    标签: javascript ajax jquery jquery-validate


    【解决方案1】:

    您的 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 :-)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        如果你使用customErrorHandler,那么实际上onCompleteHandler 永远不会被调用。假设您在customErrorHandler 的末尾当errorInfo.length == 0 时自己调用它。

        【讨论】:

        • 也试过了。没有成功。我们可以继续在Skype上讨论吗?我真的需要帮助队友
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-18
        • 1970-01-01
        • 2010-10-10
        • 2019-09-30
        • 1970-01-01
        相关资源
        最近更新 更多