How to validate clone elements in jQuery validation?

前端 未结 2 1272
失恋的感觉
失恋的感觉 2020-12-19 14:36

I am using bassistance.de/jquery-plugins

and this is default jQuery form validation service.

I am aware that question related to jQuery validation asked

相关标签:
2条回答
  • 2020-12-19 15:07

    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+"]")

    then I can safely append the generated html to the DOM and the plugin works as usual.

    REMEMBER: JQUERY VALIDATE NEEDS AN UNIQUE NAME IN THE INPUT!! I pay myself with a lot of hours lost for this.

    0 讨论(0)
  • 2020-12-19 15:12

    Solve it !

    What I used to do: after I clone the object, I called a function that creates the validation:

    function add_another_form(){
         var new_form =  jQuery(".class").clone(true);
         new_form.appendTo(".parent");
         add_validation_for_forms();//attach again the validation !
    }
    
    function add_validation_for_forms(){
        jQuery(".class").validate({....})
    

    But that was not working, so I just used a .each function in there, and it works now...

    function add_validation_for_forms(){
        jQuery(".class").each(function(){
            jQuery(this).validate({....})
    
    0 讨论(0)
提交回复
热议问题