代码之家  ›  专栏  ›  技术社区  ›  Calvin Nunes

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

  •  2
  • Calvin Nunes  · 技术社区  · 8 年前

    选择2版本: 4.0.5

    问题: 在多任务处理中(带标记:true或带预填充标记),每当我按enter键选择标记时,它都会创建标记,但会将文本保留在新标记的右侧,如果之后按另一个键,它就会消失。(仅在使用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 回复  |  直到 5 年前
        1
  •  4
  •   Eugene Gavrilov    8 年前

    选项 closeOnSelect 保持价值,而这不是预期的行为。 如果你愿意改变 关闭选择 true (默认值,仅删除 this.OptionsSelect2.closeOnSelect = false; 在您的代码中)它将按您的预期工作。

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