如何在jQuery验证中验证克隆元素? [英] How to validate clone elements in jQuery validation?
问题描述
我正在使用 bassistance.de/jquery-plugins
,这是默认的jQuery表单验证服务.
and this is default jQuery form validation service.
我知道与jQuery验证相关的问题问了太多次了,但我的问题是
有点不同.
I am aware that question related to jQuery validation asked too many times but my problem is
little bit different.
我只有一种格式,只有一个文件输入,并且正在克隆,如下所示
I have one form with one file input and i am cloning like below
var picFields = $('#file').clone(true);
并附加到表单中以上传多个文件.
and appending into the form to upload multiple file.
在克隆时,我有一个带有'required'名字的类,并添加了类似的规则
while cloning i have class with 'required' name and adding the rule like
$('.required').each(function() {
$(this).rules('add', {
required: true,
});
});
,最初像
$('#form').validate();
它仅验证第一个元素,而不验证所有动态添加的字段.
its validating only the first element not all the dynamic added fields.
推荐答案
每个输入都需要一个唯一的名称.插件无法考虑类似 name ="location []",则必须添加索引(正确的名称为name ="location [0]"). 对于克隆的元素,我首先获取html"clone().html()",然后替换[0] 带有计数器编号的我会跟踪Ex:.replace(/[0]/g,"[" + cont +]")
Every input needs an unique name. The plugins fails to consider things like name="location[]", you have to add an index (correct would have been name="location[0]"). For cloned elements I get first the html "clone().html()" then I replace the [0] with a counter number i keep track Ex: .replace(/[0]/g, "["+cont+"]")
然后,我可以安全地将生成的html附加到DOM,并且该插件照常工作.
then I can safely append the generated html to the DOM and the plugin works as usual.
记住:JQUERY VALIDATE在输入中需要一个唯一的名称!我为此付出了很多时间,为此付出了自己的时间.
REMEMBER: JQUERY VALIDATE NEEDS AN UNIQUE NAME IN THE INPUT!! I pay myself with a lot of hours lost for this.
这篇关于如何在jQuery验证中验证克隆元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!