使用动态输入标记系统

时间:2018-05-29 12:23:10

标签: php jquery dynamic tags tagging

现在我正在尝试将bootstraps标记系统与动态输入字段代码结合起来。我想为每个动态生成的字段/ div标记输入字段。

像这样:

主题1:标题
描述:这是关于标题的 标签:你好,无聊,标题

主题2:这又是一个标题 描述:再次描述了 标签:idk,help

(...)等等。

我添加其他字段的jquery文件如下所示:

    $(document).ready(function () {
        var maxGroup = 10;       
          $(".addMore2").click(function() {
          $(".tagging").css("display", "none"); 
           if ($('feld2').find('.fieldGroup').length < maxGroup) {
              var fieldHTML = '<div class="form-group fieldGroup">' + 
              $(".fieldGroupCopy").html() + '</div>';
              $('feld2').find('.fieldGroup:last').after(fieldHTML);
                } else {
                    alert('Maximum ' + maxGroup + ' groups are allowed.');
                }
            });
            //remove fields group
                $("feld2").on("click", ".remove", function () {
                $(this).parents(".fieldGroup").remove();
            });
        });

不工作的部分就是这个: 当我点击“添加”按钮时: see here 它复制了这一部分:

<div class="form-group fieldGroupCopy" style="display: none;">     
     <table>
        <tr> 
        <td class='first_td'><label for="titel"><b>Titel:</b></label></td>
       <td><input  type="text" name="description[]" class="form-control" placeholder="Title"/></td>
    <td><a href="javascript:void(0)" class="btn btn-danger remove">-</a></td>
    </tr>
    <tr>
        <td><b>Beschreibung:</b></td>
         <td><textarea type="text" name="description[]" class="form-control" placeholder="Beschreibung des Themas"/></textarea></td>
       </tr>
            <tr>
             <td><label for="Tags"><b>Tags</b></label></td>
                <td colspan='2'>
                    div class="form-group">
                            <input type="text" name='tags_WiBe[]' placeholder='Add Tags' data-role="tagsinput" class="form-control" />
                        </div>
                        </td>
                        </tr>
               </table>
              </div>

但标记系统在复制的字段中不起作用。 它只能在div“form-group fieldGroup”之外工作。

see here 每次我输入一个表格想要提交的标签,但这不是我想要的。

请帮帮我......

编辑:

这是一个更清楚地展示问题的方法。 https://jsfiddle.net/t5vrLsur/#&togetherjs=pbAhjTR1t1 我知道这不是最美丽的结构。 不要太难我。 :(

1 个答案:

答案 0 :(得分:0)

问题在于插件本身会关注DOM准备好的第一次初始化。在您的情况下,您希望在每次添加卡后运行它。

您可以通过在新卡上下文中简单运行插件初始化来实现此目的。

此外,您必须从模板中的data-role中删除input属性,因为正如我们所说,此属性意味着自动初始化插件,但在模板的情况下我们我们将加载卡后手动完成。

以下是相关内容:

// wrap the html with jQuery so you could delete the inputtags wrapper later
var fieldHTML = $('<div class="form-group fieldGroup">' + $(".fieldGroupCopy").html() + '</div>');
$('feld2').find('.fieldGroup:last').after(fieldHTML);
// initialise again
fieldHTML.find('input').tagsinput();

工作演示

http://output.jsbin.com/dabijac/

此外,您的代码中有几个jQuery引用会导致问题。您已删除它或使用$.noConflict来保留它们。

相关问题