在自定义方法中使用.element(element)时,jQuery验证失败(jQuery validation fails when using .element(element) within c

编程入门 行业动态 更新时间:2024-10-27 16:28:25
自定义方法中使用.element(element)时,jQuery验证失败(jQuery validation fails when using .element(element) within custom method)

我有一个自定义规则,应通过验证此依赖的其他输入来检查某些依赖项。 当我这样做时,所有其他输入的验证似乎被忽略了。

这是我的自定义验证规则:

jQuery.validator.addMethod("checkDependencies", function (value, element) { var valid1 = jQuery('form#add-lottery-form').validate().element('#input-1'); var valid2 = jQuery('form#add-lottery-form').validate().element('#input-2'); if (valid1 && valid2) { return true; } else { return false; } }, 'dependencie error');

我创建了一个jsfiddle来显示我的问题: http://jsfiddle.net/AQcrW/

重现步骤:

在input4中键入内容(此输入是具有自定义规则“checkDependencies”的内容)[JavaScript部分中的第1行] 由于JS第2行和第3行的调用,显示了input1和input2上的错误 将正确的值插入input1和input2 点击提交 !!认识到input3未经过验证!!

重新播放小提琴

点击提交 所有字段都按预期验证

这是我的错还是jQuery验证中的错误?

I have a custom rule that should check some dependencies by validating the other inputs this one depends on. When I do stuff like that validation for all other inputs seems to be ignored.

This one is my custom validation rule:

jQuery.validator.addMethod("checkDependencies", function (value, element) { var valid1 = jQuery('form#add-lottery-form').validate().element('#input-1'); var valid2 = jQuery('form#add-lottery-form').validate().element('#input-2'); if (valid1 && valid2) { return true; } else { return false; } }, 'dependencie error');

I have created a jsfiddle to show my problem: http://jsfiddle.net/AQcrW/

steps to reproduce:

type something in input4 (this input is the one with custom rule "checkDependencies") [line 1 in JavaScript-part] errors on input1 and input2 are shown due to calls in JS line 2 and line 3 insert correct values to input1 and input2 click submit !!recognize that input3 was not validated!!

rerun the fiddle

click submit all fields are validated as expected

Is this my fault or is it a bug in jQuery validation?

最满意答案

经过一些调试和概述插件代码我有一个解决我的问题的方法。 我扩展了验证器插件,以便能够调用内部.check(元素)函数。 这个函数只返回true / false。 它不是一个完美的解决方案,因为它在表单提交上做了两次,但至少它到目前为止工作..所以这里是我添加的代码: 不需要代码 - 阅读编辑!

jQuery.extend(jQuery.validator.prototype, { check: function (element) { return this.check(element); } });

这是工作的fidde: http://jsfiddle.net/HrPRL/ 跟随我的想法,也许看一下即将进行的讨论: https://github.com/jzaefferer/jquery-validation/issues/579 编辑: 正如我现在意识到的,我非常酷的 jQuery扩展没有任何影响.. 因为.check()方法是原型对象的一部分,您可以访问它。 所以不需要上面的代码。 编辑2: 这就是我最终的结果。 它工作正常,但我认为有些东西可以优化。 有什么更好的想法吗? http://jsfiddle.net/HrPRL/6/ 我完成了..这足以满足我的意图: http : //jsfiddle.net/HrPRL/8/

After some debugging and overview the plugins code i have a solution for my problem. I extended the validator plugin to be able to call the internal .check(element) function. this function just returns true/false. Its not a perfect solution because it does some stuff twice on form submit but at least it works so far.. so here is the code I added: no need for the code - read edit!

jQuery.extend(jQuery.validator.prototype, { check: function (element) { return this.check(element); } });

and here is the working fidde: http://jsfiddle.net/HrPRL/ to follow my thoughts and maybe check out upcoming discussion: https://github.com/jzaefferer/jquery-validation/issues/579 Edit: As i realized right now my pretty cool jQuery extend doesn't have any effect.. because the .check() method is part of the prototype object you can access it already. So the code above is not needed. Edit 2: this is what I ended up with. It's working but I think some stuff could be optimized. any better ideas to do it? http://jsfiddle.net/HrPRL/6/ i am done.. thats enough for my intention: http://jsfiddle.net/HrPRL/8/

更多推荐

本文发布于:2023-07-24 01:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   方法   element   jQuery   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!