如何创建通用的jQuery验证和重置函数调用 [英] How to create a universal jquery validate and reset function call

查看:146
本文介绍了如何创建通用的jQuery验证和重置函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery validate插件为应用程序中的所有表单创建一种验证方法.使用jquery each()函数循环每种形式并应用该方法.我想使用相同的每种方法来实现重置.我该怎么做?

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?

这是有效的代码:

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

这不是代码

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

请告知.

推荐答案

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

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();
   });
});

这将对其进行更正,因此您将重置每个<form>一次,并将其重置为最后一次<form> n次.

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

这篇关于如何创建通用的jQuery验证和重置函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆