[英]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.