使用jQuery Validate插件验证具有相同名称的多个表单字段 [英] Validating multiple form fields with identical names using jQuery Validate plugin

查看:101
本文介绍了使用jQuery Validate插件验证具有相同名称的多个表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用jQuery Validate插件和验证多个表单字段).

I managed to validate multiple form fields with identical names, such as first_name[], using jQuery Validate plugin and this workaround (quoted here).

但是,错误消息仅显示在该字段的第一个实例中,而不显示在下一个实例中.

However error messages are displayed only for the first instance of a field, not the next ones.

那是为什么?

为记录起见,以上链接中提供的解决方案包括编辑jquery.validate.js并将checkForm函数内容更改为:

For the record, the solution presented in the links above consists in editing jquery.validate.js and change the checkForm function content into:

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

然后在调用插件时使用此类参数:

Then using this kind of parameters when calling the plugin:

rules: {
"field_name[]": "required"
}

推荐答案

引用OP :

"我设法使用jQuery Validate插件验证具有相同名称的多个表单字段,例如first_name[] (在此处引用)."

"I managed to validate multiple form fields with identical names, such as first_name[], using jQuery Validate plugin and this workaround (quoted here)."

强烈建议您不要编辑任何插件的源代码.

I strongly recommend against editing the source code of any plugin.

  • 您必须确保更新时不会覆盖它.

  • You have to make sure you never over-write it when updating.

您会为下一个从事该项目的人造成困惑.

You create confusion for the next person that works on the project.

您可以引入许多未知的错误.

You may introduce any number of unknown bugs.

这是一个非常流行且经过全面测试的插件.编辑源代码,然后忽略所有这些……您最好编写自己的插件.

This is a very popular and thoroughly tested plugin. Edit the source and forget about all of that... you might as well write your own plugin.

此外,您引用的黑客已有3年之久了……您怎么知道它在最新版本的插件上仍然可以正常工作(如果可以的话)?

Besides, the hack you're quoting is more than three years old... how do you know that it's still supposed to work (if it ever did work) properly on the latest version of the plugin?

引用OP :

但是,错误消息仅显示在该字段的第一个实例中,而不显示下一个.

"However error messages are displayed only for the first instance of a field, not the next ones.

那是为什么?"

当您有多个共享相同的name字段时,此插件会发生这种情况...仅识别第一个实例.没有解决方法.

That's exactly what happens with this plugin when you have multiple fields sharing the same name... only the first instance is recognized. There is no workaround for this.

该插件旨在通过其name属性来跟踪表单输入.我不知道如何解决这个问题,因为您仍然需要跟踪所有内容而无需重复.

The plugin was designed to keep track of the form inputs via their name attributes. I don't know how any hack is going to get around that, since you still need to keep track of everything without duplication.

但是,您根本不需要执行任何此操作.该插件旨在处理属于数组一部分的字段name.可以接受"field[1]""field[2]"等.

However, you should not need to do any of this at all. The plugin is designed to handle field name's that are part of an array... "field[1]", "field[2]", etc. are acceptable.

  • 如果表单是静态的,只需创建唯一的字段名称.

  • If your form is static, simply create unique field names.

如果表单是动态的,请使用增量计数器正确创建数组索引,以便每个字段以唯一的name结尾.

If your form is dynamic, use an incremental counter to properly create an array index so that every field ends up with a unique name.

这篇关于使用jQuery Validate插件验证具有相同名称的多个表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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