如何创建通用 jquery 验证和重置函数调用
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有
var
关键字,您将validator
设置为全局变量,并在每个循环中更改它的设置(因此它将被设置为末尾的最后一个表单),只需在声明时添加var
即可,如下所示:这将更正它,因此您将重置每个
Without the
var
keyword, you're settingvalidator
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 addvar
when declaring it, like this:This will correct it so you'll reset each
<form>
once, rater than the last<form>
n
times.也许试试这个?
try this maybe?