1

I have a modal that is launched by clicking any one of several images on a page where there is an input field to which I am trying to apply bootstrap-tagsinput.

$(document).on("click","a.lazy",function(){        
        $("#form").find(".title").attr('value',$(this).find(".title").val());
        $("#form").find(".title").attr('data-role','tagsinput');
        $("#form").modal("show");
});

I am assigning the value and data-role attributes with lazyload to the following input field:

<div class="input-group">  
   <span class="input-group-addon">Tags</span>
   <input type="text" class="form-control title" placeholder="Enter Tags for the Image" name="title">
</div>

Once I launch the modal I can see that the attributes are present but the tags are not applied. What am I doing wrong?

<input type="text" class="form-control title" placeholder="Enter Tags for the Image" name="title" value="yellow" data-role="tagsinput">
4

1 回答 1

1

仅添加data-role到输入字段是不够的 - 因为您正在动态添加它,您需要像这样初始化 bootstrap-tags-input:

$(document).on("click","a.lazy",function(){        
        $("#form").find(".title").attr('value',$(this).find(".title").val());
        $("#form").find(".title").attr('data-role','tagsinput');

        // Destroy all previous bootstrap tags inputs (optional)
        $('input[data-role="tagsinput"]').tagsinput('destroy');
        // Create the bootstrap tag input UI
        $('input[data-role="tagsinput"]').tagsinput('items');

        $("#form").modal("show");
});
于 2014-09-03T19:37:36.490 回答