如何创建通用 jquery 验证和重置函数调用

发布于 2024-10-05 06:53:06 字数 469 浏览 1 评论 0原文

我正在使用 jquery 验证插件为应用程序中的所有表单创建验证方法。使用 jqueryeach() 函数循环每个表单并应用该方法。我想使用相同的每种方法来实现重置。我怎样才能做到这一点?

这是有效的代码:

 $("form").each(function () { 
        $(this).validate(); 
    });

这是无效的代码,

   $("form").each(function () { 
        validator = $(this).validate(); 
        validator.resetForm();
        $("#reset").click(function () {
            validator.resetForm();
        });
    });

请告知..

I am using the jquery validate plugin to create a validation method for all forms in an application. Using the jquery each() function to cycle each form and applyt the method. I want to achieve the reset using the same each method. how can i accomplish this?

This is the code that works:

 $("form").each(function () { 
        $(this).validate(); 
    });

This is the code that doesn't

   $("form").each(function () { 
        validator = $(this).validate(); 
        validator.resetForm();
        $("#reset").click(function () {
            validator.resetForm();
        });
    });

Please advise..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-10-12 06:53:06

如果没有 var 关键字,您将 validator 设置为全局变量,并在每个循环中更改它的设置(因此它将被设置为末尾的最后一个表单),只需在声明时添加 var 即可,如下所示:

$("form").each(function () { 
   var validator = $(this).validate();  //add var here
   validator.resetForm();
   $("#reset").click(function () {
       validator.resetForm();
   });
});

这将更正它,因此您将重置每个

一次,而不是最后 n 次。

Without the var keyword, you're setting validator as a global variable, and changing what it's set to with each loop (so it'll be set the to the validator of the last form at the end), just add var when declaring it, like this:

$("form").each(function () { 
   var validator = $(this).validate();  //add var here
   validator.resetForm();
   $("#reset").click(function () {
       validator.resetForm();
   });
});

This will correct it so you'll reset each <form> once, rater than the last <form> n times.

鹤仙姿 2024-10-12 06:53:06

也许试试这个?

$("form").each(function () { 
var validator = $(this);
validator.validate(); 
validator.resetForm(); 
$("#reset").click(function () { 
    validator.resetForm(); 
}); 
 });

try this maybe?

$("form").each(function () { 
var validator = $(this);
validator.validate(); 
validator.resetForm(); 
$("#reset").click(function () { 
    validator.resetForm(); 
}); 
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文