How to use jQuery to add form elements dynamically

前端 未结 3 1738
后悔当初
后悔当初 2020-11-28 09:17

Here is my html:

3条回答
  •  迷失自我
    2020-11-28 10:18

    Try this way:-

    Demo

    HTML

    Create a template extraPersonTemplate and use it to construct further. Add a container tyo your content section.

    JS

      $(document).ready(function () {
         $('
    ', { 'class' : 'extraPerson', html: GetHtml() }).appendTo('#container'); //Get the html from template $('#addRow').click(function () { $('
    ', { 'class' : 'extraPerson', html: GetHtml() }).hide().appendTo('#container').slideDown('slow');//Get the html from template and hide and slideDown for transtion. }); }) function GetHtml() //Get the template and update the input field names { var len = $('.extraPerson').length; var $html = $('.extraPersonTemplate').clone(); $html.find('[name=firstname]')[0].name="firstname" + len; $html.find('[name=lastname]')[0].name="lastname" + len; $html.find('[name=gender]')[0].name="gender" + len; return $html.html(); }

    Small Css

    .extraPersonTemplate {
        display:none;
    }
    

提交回复
热议问题