Search code examples
jqueryspringspring-mvcspring2.x

jquery dynamic form elements for spring form binding


I am facing issues with dynamically binding spring form elements in JSP using jquery. I am trying to add dynamic rows to a table which has different form elements. Demo: http://jsfiddle.net/ninadhere/f7ZdK/

Issue here is newly added form elements are havng id's & name attribute as "userName[0]1" instead of "userName[1]" hence they are not getting binded to spring form. I am using a AbstractWizardController and trying to binding Dynamic list of User's on Step1 and then using them in Step2.

Is their a way to systematically clone the form elements like dropdown, checkbox and text.

Can someone guide me on using this correctly.


Solution

  • pass true while cloning the element will copy all event with the cloned element.

    from http://api.jquery.com/clone/

    .clone( [withDataAndEvents] ) withDataAndEventsA Boolean indicating whether event handlers should be copied along with the elements. As of jQuery 1.4, element data will be copied as well.

    Replacing integer value of id:

      id.replace(/\[\d*\]$/, "["+ i +"]")// will replace 'userName[0]' with userName[{i}]