按Enter键正在创建标签,但将文本保留在Select2中

时间:2018-01-04 13:29:12

标签: javascript jquery html jquery-select2

选择2版本: 4.0.5

问题: 每次当我按Enter键选择标签时,在多标签(带标签:true或预填充标签)中,它会创建标签但是将文本保持在新标签的正面,如果我按下另一个键后它就会消失。 (仅适用于Enter键)

示例:
enter image description here

正如您所看到的,当我对标记进行数字处理并按Enter键时,它会将标记创建为正常行为,但它会将原始文本保留在新标记的右侧...

这是正常行为吗?或者我该怎么办?

我的Select2代码:

 var editorTagElement = document.createElement("select");
 editorTagElement.className('myEditor');
 editorTagElement.setAttribute("multiple", "multiple");
 editorTagElement.style.overflow = 'auto';
 editorTagElement.style.fontSize = '13px';

 this.OptionsSelect2 = {};
 this.OptionsSelect2.placeholder = "";
 this.OptionsSelect2.language = ReturnCustomLanguage();
 this.OptionsSelect2.formatNoMatches = function () {
        return '';
 }; 
 this.OptionsSelect2.allowClear = true;
 this.OptionsSelect2.minimumInputLength = 2;
 this.OptionsSelect2.tags = true;
 this.OptionsSelect2.tokenSeparators = [' '];          
 this.OptionsSelect2.createTag = function (params) {              
       var term = $.trim(params.term);
       if (term.length < 2) {
              return null;
       }
       term = term.replace(/ /g, "");         
       return {
            id: term,
            text: term,
            newTag: true
       }
 };
 this.OptionsSelect2.closeOnSelect = false;

 $('.myEditor').select2(this.OptionsSelect2);

在此之后,我将编辑器附加到正文或我的HTML中的另一个地方......

1 个答案:

答案 0 :(得分:3)

选项closeOnSelect保留值,而不是预期的行为。 如果您将closeOnSelect更改为true(默认值,只需删除代码中的this.OptionsSelect2.closeOnSelect = false;),它就可以正常运行。

要在添加值后保持弹出窗口打开,您应该编写类似于github issue

中建议的处理程序