简体   繁体   English

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

[英]Pressing Enter is creating the Tag but keeping the Text in Select2

Select2 Version: 4.0.5 Select2 版本: 4.0.5

Problem: In a multitag (with tags: true or with pre-filled tags) everytime when i hit enter to select the tag, it creates the tag but keeps the text right on side of the new tag, it just disappear if I press another key after.问题:在多标签(带有标签:true 或带有预填充标签)中,每次我按 Enter 键选择标签时,它都会创建标签但将文本保留在新标签的右侧,如果我按下另一个标签,它就会消失关键之后。 (Happens only with Enter key) (仅使用 Enter 键发生)

Example:示例:
在此处输入图片说明

As you can see, when I type a tag and press Enter, it creates the tag (the expected behavior) but, it keeps the original text in the right side of the new tag...如您所见,当我键入一个标签并按 Enter 时,它会创建该标签(预期行为),但是,它将原始文本保留在新标签的右侧...

Is it a normal behavior?这是正常行为吗? Or what should I do?或者我该怎么办?

My Select2 Code:我的 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);

After this, I append the editor to body or to another place in my html...在此之后,我将编辑器附加到正文或我的 html 中的另一个位置...

Option closeOnSelect is keeping the value, while it's not an expected behaviour.选项closeOnSelect保留该值,但它不是预期的行为。 If you will change closeOnSelect to true (default value, just remove this.OptionsSelect2.closeOnSelect = false; in your code) it will work as you expect.如果您将closeOnSelect更改为true (默认值,只需在您的代码中删除this.OptionsSelect2.closeOnSelect = false; ),它将按您的预期工作。

To keep popup open after adding a value, you should write a handler similar to suggested in the github issue要在添加值后保持弹出窗口打开,您应该编写一个类似于github 问题中建议的处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM