5

我无法让默认的 Bootstrap-TagsInput confirmKeys(即enter=13comma= 188)开箱即用。无论有没有Typeahead.js都是如此。确认键允许您通过单击该键来创建标签。

我认为问题在于标签是字符串还是对象。如果您查看Tagsinput 演示,“Typeahead”示例允许使用默认创建标签confirmKeysentercomma,但其下方的“对象作为标签”示例不允许。

知道如何confirmKeys使用对象标签进行工作吗?

4

3 回答 3

3

我必须编辑 Bootstrap-tagsinput 库才能完成这项工作。

这是我在库中添加/注释的内容:

 //self.options.freeInput = false; //notice commented out

//... (lots of lines between)

if (self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)) {
    // Only attempt to add a tag if there is data in the field

    if (text.length !== 0) {
       //<<<<< BEGIN code block added
       //self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text); //notice commented out

       var item2 = self.$input.val();
       if (self.objectItems) {
         var beforeFreeInputItemAdd = $.Event('beforeFreeInputItemAdd', { item: item2, cancel: true });
         self.$element.trigger(beforeFreeInputItemAdd);
         if (beforeFreeInputItemAdd.cancel)
           return;

         item2 = beforeFreeInputItemAdd.item;
       }

       self.add(item2);
       self.$input.val(''); 
       //  $input.val(''); //>>>>>> END code block added
    }

}

然后到代码库中想要使用这个库修改的任何地方,我添加了这个:

var id_increment = 1;
$("#my-tagsinput-field").on('beforeFreeInputItemAdd', function(event) {

    event.item = {'name': event.item, 'id': 'new-'+id_increment};
    event.cancel = false;
    id_increment++;

});
于 2015-12-27T19:27:32.010 回答
0

对我来说,解决方案是在配置中有freeInput,例如

$( 'input[type="tags"]' ).tagsinput(
{
    typeaheadjs: [{
        minLength: 1,
        highlight: true
    },
    {
        limit: 99,
        name: type,
        displayKey: 1,
        valueKey: 'name',
        source: sourcefunc,
        templates: { suggestion: suggestionsfunc }
    }],
    freeInput: true
});

并在 tagsinput 源代码中将以下内容更改为false

cancelConfirmKeysOnEmpty: false,

这对我来说在第 24 行。

于 2021-02-25T09:51:32.240 回答
0

2022 年,Bootstrap 版本 2.3.1:

https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/

把这段代码:

$('input').tagsinput({
  confirmKeys: [13, 44, 32]
});

在里面<script>,但在外面$(document).ready({...

键:13 - 输入;44 - 逗号;32 - 空格键。

您也应该更改$('input')$('your_input_class_or_id')并且不要忘记 # 或 。在开始$('#your_...$('.your_...

于 2022-01-27T11:13:46.507 回答