2014-01-17 97 views
0

在我的工作我曾經回答這個問題:如何驗證JQuery的克隆形式

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

在打電話之前,再次驗證,你需要擺脫附着在克隆形式的舊驗證數據。

所以你的代碼的末尾是這樣的:

  .prependTo('#attendees') // add to container 
      .removeData('validator') //get rid of cloned validator 
      .validate(
       // rules 
      ); 
0

我用這個方法:

//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() 

希望這有助於。

0

嘗試сlone(true),它會將事件處理程序附加到克隆的對象,這可能會有所幫助。