如何验证JQuery克隆表单
在我的工作中,我曾经回答过这个问题: 如何使用JQuery验证验证克隆表单 我尝试了以下方法,但无效:如何验证JQuery克隆表单,jquery,forms,validation,clone,increment,Jquery,Forms,Validation,Clone,Increment,在我的工作中,我曾经回答过这个问题: 如何使用JQuery验证验证克隆表单 我尝试了以下方法,但无效: $(document).ready(function () { $(function () { var template = $('#attendees .attendee:first').clone(), attendeesCount = 1; var addAttendee = function () {
$(document).ready(function () {
$(function () {
var template = $('#attendees .attendee:first').clone(),
attendeesCount = 1;
var addAttendee = function () {
attendeesCount++;
var nativeTemplate = template.clone();
var attendee = nativeTemplate.find(':input').each(function () {
var newId = this.id.substring(0, this.id.length - 1) + attendeesCount;
$(this).prev().attr('for', newId); // update label for (assume prev sib is label)
this.name = this.id = newId; // update id and name (assume the same)
}).end() // back to .attendee
.attr('id', 'att' + attendeesCount) // update attendee id
.prependTo('#attendees') // add to container
.validate(
// rules
);
};
$('.add').click(addAttendee); // attach event
});
});
在再次调用validate之前,需要清除附加到克隆表单的旧验证器数据 因此,代码的结尾如下所示:
.prependTo('#attendees') // add to container
.removeData('validator') //get rid of cloned validator
.validate(
// rules
);
我采用了这种方法:
//Clone the form
var clonedForm = $('.original-form').clone(true);
//Get original form & validator
var originalform = $('.original-form').find('form');
var originalValidator = originalform.data('validator');
//Set validator to cloned form in popup
originalValidator.currentForm = clonedForm;
//Re-Set the validator to the cloned form
clonedForm.data('validator', originalValidator);
//Now you can validate the clonedForm by calling "valid()".
$(clonedForm).valid()
希望这能有所帮助。尝试一下C lonetrue,它会将事件处理程序附加到克隆对象上,可能会有所帮助