在我的工作中,我常常回答这个问题:
JQuery clone form and increment
如何使用JQuery验证验证克隆表单?
我尝试了以下操作,但它不起作用:
$(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
});
});
答案 0 :(得分:0)
在再次调用validate之前,您需要删除附加到克隆表单的旧验证程序数据。
所以代码的结尾如下所示:
.prependTo('#attendees') // add to container
.removeData('validator') //get rid of cloned validator
.validate(
// rules
);
答案 1 :(得分:0)
尝试сlone(true),它会将事件处理程序附加到克隆对象,这可能有所帮助。
答案 2 :(得分:-1)
我使用了这种方法:
//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()
希望这有帮助。