简体   繁体   English

jQuery验证克隆元素

[英]Jquery Validation on cloned elements

I am using the following JQuery validation: 我正在使用以下JQuery验证:

http://bassistance.de/jquery-plugins/jquery-plugin-validation/ http://bassistance.de/jquery-plugins/jquery-plugin-validation/

I have the following element: 我有以下要素:

<div class="form-item">
  <label for="Reference_name" class="required">Name: <span class="required">*</span></label>                        
   <input name="Reference[name][]" class="form-input validate {validate:{required:true,minLength:2,messages:{required:'Your name is required',minLength:'Your name is too short'}}}" id="Reference_name" type="text">                                        
</div>

I have cloned the element but the validation is only appearing on the first element. 我已经克隆了元素,但是验证仅出现在第一个元素上。 I would like it to validate against the second too and show the error message label. 我也想针对第二个进行验证并显示错误消息标签。

Can someone help with this please. 有人可以帮忙吗?

elements must be unique 元素必须是唯一的

<label for="Reference_name" class="required">Name:<span class="required">*</span></label>            
<input type="text" id="Reference_name" name="Reference[Name]" id="Reference_name" required="required" maxlength="255" />

File Js 文件Js

$(document).ready(function() {
 validate_form();
});     


function validate_form() {
    $("#id_of_your_form").validate({
    rules: {
        'Reference_name': {
            required:true,
            minlength: 2,                
            }
        },
    },
    messages: {
        'Reference_name': {
            required:"Your name is required",
            minLength:'Your name is too short'
        },           
    }

});
}

if you want to compare two fields http://docs.jquery.com/Plugins/Validation/Methods/equalTo#other 如果要比较两个字段http://docs.jquery.com/Plugins/Validation/Methods/equalTo#other

Put your validation function in a global variable like this: 将您的验证函数放在这样的全局变量中:

var validate_form_function = function(){
                if($(".app-form").length > 0){
                  $('.app-form').validate({
                    rules: {
                            comment_message: {
                                required: true,
                                minlength: 2
                            }
                    },

                    messages: {
                            comment_message: {
                                required: "Your message",
                                minlength: "Your message"
                            }
                    }
                  });
                }
            };

Then revalidate your cloned form with the function like this : validate_form_function(); 然后使用以下函数重新验证克隆的表单:validate_form_function();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM