Jquery tagsinput 不会在粘贴输入时自动创建标签

Jquery tagsinput not creating tags automatically on pasting input

我正在使用 jquery.tagsinput 并希望能够粘贴以逗号或 space 分隔的电子邮件地址列表。使用类似 https://github.com/xoxco/jQuery-Tags-Input/issues/22 的东西,但在我按下 Enter 之前它不会添加它们 - 尝试触发按键 Enter 事件,但它不起作用。模糊事件也不走运(如下所示)。有什么想法吗?

Flat-UI 标签基于这个库,我正在尝试实现非常相似的行为。

var tidyTags = function(e) {
  var tags = (e.tags).split(/[ ,]+/);
  var target = $(e.target);

  for (var i = 0, z = tags.length; i<z; i++) {
      var tag = $.trim(tags[i]);
      if (!target.tagExist(tag)) {
          target.addTag(tag);
      }
  }
  $('#' + target[0].id + '_tag').trigger('focus');

  //This doesn't work.
  target.blur();

};

$("#tagsinput").tagsInput({
    onAddTag : function(tag){
      if(tag.indexOf(',') > 0) {
          tidyTags({target: '#tagsinput', tags : tag});
      }
    },
});

好的,终于找到解决办法了:

DEMO HERE

只需在粘贴时向您的 textbox 添加一个侦听器,不要在初始化期间设置 onAddTag,只需按如下方式简单调用即可:

$("#tagsinput").tagsInput();//Initialization

$("#tagsinput_tag").on('paste',function(e){
    var element=this;
    setTimeout(function () {
        var text = $(element).val();
        var target=$("#tagsinput");
        var tags = (text).split(/[ ,]+/);
        for (var i = 0, z = tags.length; i<z; i++) {
              var tag = $.trim(tags[i]);
              if (!target.tagExist(tag)) {
                    target.addTag(tag);
              }
              else
              {
                  $("#tagsinput_tag").val('');
              }
         }
    }, 0);
});

Some points to note:

  • paste method will only fire if text is selected in Firefox
  • tagsinput will hide your #tagsinput textbox and adds its own input textbox and thus you need to call paste event on #tagsinput_tag textbox and the structure of the element will be as shown in below image:

非常感谢这个鼓舞人心的解决方案!

上面的代码我遇到了一些问题。我正在使用 并且不想通过 ID 访问元素。

以下是 声明中对我有用的内容,以防有人感兴趣:

link: function(scope, el, attr){

    /* initializing element */
    var $el = angular.element(el);
    $el.tagsinput({...}); //<-- initialize as desired

    /* getting the "_tag" input  */
    var $elTag = $el.tagsinput('input');

    /* attaching event to "_tag" input */
    $elTag.on('paste', function (e) {
        var element = this;
        setTimeout(function () {
            var text = $(element).val();
            var target = $el;
            $elTag.val(''); // <--- removes the pasted value containing the ","
            var tags = (text).split(/[ ,]+/);
            for (var i = 0, z = tags.length; i < z; i++) {
                var tag = $.trim(tags[i]);
                if(target.tagsinput('items').indexOf(tag) < 0){ // <-- I got "'tagExist' not a function" error
                    target.tagsinput('add',tag);
                }
            }
        }, 0);
    });
}

首先你应该导入这个文件https://github.com/xoxco/jQuery-Tags-Input/tree/master/src

这是我们的脚本代码。

 $(function () {
        $("#EditorInputs").val($("#Editors").val());
        $('#EditorInputs').tagsInput({
            'height': '50px',
            'width': 'auto',
            'defaultText': 'Editor',
            'removeWithBackspace': true,
            'delimiter': [','],
            'onChange': function () {
                $("#Editors").val($('#EditorInputs').val());
            }
        });
    });

如果您使用的是 MVC

 <div class="form-group">
            @Html.LabelFor(m => m.Editors, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.HiddenFor(m => m.Editors)
                <input id="EditorInputs" value="" />
                @Html.ValidationMessageFor(m => m.Editors, "", new { @class = "text-danger" })
            </div>
        </div>