如何添加标签+文本框

时间:2019-04-07 15:57:19

标签: jquery symfony

您好,我使用的是symfony 4,在汽车和关键字之间有一个OneToMany关系。单击按钮时,我想添加和删除label + input

$(document).ready(function () {
     var $container = $('#car_keywords');
     var $index = $container.find(':input').length;    

    $('#key2').click(function(e){
           e.preventDefault();
         $('data-prototype:last-child').remove();
             $index--;
         });
     $('#key1').click(function (e) {
        e.preventDefault();
         addkeyword($container);
     });
     function addkeyword($container) {

         var template = $container
             .attr('data-prototype')
             .replace(/__name__label__/g, 'mot clé n°' + ($index + 1))
         .replace(/__name__/g, $index);


    var $prototype = $(template);
     $container.append($prototype);
     $index++;

    }

    });  

这是add.html.twig,我要在其中添加动态字段:

{% extends "base.html.twig" %}

{% block body %}
<div class="container">
    <div class="section">
        <div class="row">
            <div class="col s12 m12">
                 <h1> l'ajout d'une voiture  </h1>   
                {% include "library/form.html.twig" %}
                <button  id="key1"  class="btn">Add keyword ?</button>
                <button  id="key2"  class="btn">remove keyword ?</button>
                <button type="submit" class="btn">Save</button>
                {{ form_end(form) }}
            </div>

        </div>
    </div>
</div>
{% endblock %}

0 个答案:

没有答案