输入元素数组的 JQuery 验证

发布于 2024-10-08 22:01:07 字数 806 浏览 0 评论 0原文

我需要验证输入文本元素(里程)的数组:例如:

<tbody>
  <c:forEach items="${list}" var="item">
        <tr> 
             <!--some other columns---> 
             <td align="left"><input type="text" name="mileage" value="" /></td>
        </tr>
   </c:forEach>                       
</tbody>

验证脚本如下 -

$(document).ready(function(){

        $("#form1").validate({
            rules: {
                mileage: {
                    required: true

                         }
                },            
            submitHandler: function(form) {
                form.submit();
            }

        });       
    });

现在的问题是 .validate.js 仅验证里程的第一个元素。我能做些什么?如何使插件验证所有输入文本?

我希望你能帮助我。

I need to validate an array of input text elements (mileage): For example:

<tbody>
  <c:forEach items="${list}" var="item">
        <tr> 
             <!--some other columns---> 
             <td align="left"><input type="text" name="mileage" value="" /></td>
        </tr>
   </c:forEach>                       
</tbody>

The script for validation is as below -

$(document).ready(function(){

        $("#form1").validate({
            rules: {
                mileage: {
                    required: true

                         }
                },            
            submitHandler: function(form) {
                form.submit();
            }

        });       
    });

Now the problem is that the .validate.js only validates the first element of mileage. What can I do? How can I make the plugin validate all of the inputs text ?

I hope you can help me out.

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

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

发布评论

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

评论(4

迷路的信 2024-10-15 22:01:07

在jquery.validate.js中,我们可以找到一个名为checkForm的函数,我们需要对其进行如下修改:

checkForm: function() {
                this.prepareForm();
                for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                    if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
                        for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
                            this.check( this.findByName( elements[i].name )[cnt] );
                        }
                        } else {
                    this.check( elements[i] );
                }
                }
            return this.valid();
    }

In jquery.validate.js, we can find a function named checkForm, we have to modify it as below:

checkForm: function() {
                this.prepareForm();
                for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                    if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
                        for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
                            this.check( this.findByName( elements[i].name )[cnt] );
                        }
                        } else {
                    this.check( elements[i] );
                }
                }
            return this.valid();
    }
薯片软お妹 2024-10-15 22:01:07

基于 eddy 答案,此函数还考虑了 ignore 设置。

        checkForm: function() {
            this.prepareForm();
            for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
                if (checkingElements.length !== undefined && checkingElements.length > 1) {
                    for (var cnt = 0; cnt < checkingElements.length; cnt++) {
                        this.check( checkingElements[cnt] );
                    }
                } else {
                    this.check( elements[i] );
                }
            }
            return this.valid();
        },

Based on eddy answer, this function takes into count also the ignore setting.

        checkForm: function() {
            this.prepareForm();
            for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
                if (checkingElements.length !== undefined && checkingElements.length > 1) {
                    for (var cnt = 0; cnt < checkingElements.length; cnt++) {
                        this.check( checkingElements[cnt] );
                    }
                } else {
                    this.check( elements[i] );
                }
            }
            return this.valid();
        },
梦罢 2024-10-15 22:01:07

在这些情况下,您必须循环遍历,如下所示:

$(document).ready(function() {
    $("#form1").validate({
        submitHandler: function(form) {
            form.submit();
        }
    });  
    $("#form1 input[name='mileage']").each(function() {
       $(this).rules("add", { required: true });
    });  
});

.rules( ) 只影响第一个匹配,所以你需要一个 .each() 在那里循环遍历并向所有匹配添加任何规则。

You have to loop through in these cases, like this:

$(document).ready(function() {
    $("#form1").validate({
        submitHandler: function(form) {
            form.submit();
        }
    });  
    $("#form1 input[name='mileage']").each(function() {
       $(this).rules("add", { required: true });
    });  
});

.rules() only affects the first match, so you need a .each() on there to loop through and add any rules to all matches.

摘星┃星的人 2024-10-15 22:01:07

使用jquery validate 1.19.1,修改第465行的jquery.validate.js文件,替换成这个函数应该就可以了。

checkForm: function() {
            this.prepareForm();
            for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
                if (checkingElements.length !== undefined && checkingElements.length > 1) {
                    for (var cnt = 0; cnt < checkingElements.length; cnt++) {
                        this.check( checkingElements[cnt] );
                    }
                } else {
                    this.check( elements[i] );
                }
            }
            return this.valid();
        },

我希望能有所帮助;)

Using jquery validate 1.19.1 and modifying the jquery.validate.js file on line 465, replacing with this function should be fine.

checkForm: function() {
            this.prepareForm();
            for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                var checkingElements = this.findByName( elements[i].name ).not(this.settings.ignore);
                if (checkingElements.length !== undefined && checkingElements.length > 1) {
                    for (var cnt = 0; cnt < checkingElements.length; cnt++) {
                        this.check( checkingElements[cnt] );
                    }
                } else {
                    this.check( elements[i] );
                }
            }
            return this.valid();
        },

I hope to be of help ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文