选择2版本:4.0.5
问题: 在多标签(带有标签:true或带有预填充标签)中,每次当我按回车键选择标签时,它都会创建标签,但将文本保留在新标签的右侧,如果我在之后按另一个键,它就会消失。 (仅适用于 Enter 键)
如您所见,当我键入标签并按 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 中的其他位置...
最佳答案
选项closeOnSelect
保留该值,但这不是预期的行为。
如果您将 closeOnSelect
更改为 true
(默认值,只需在代码中删除 this.OptionsSelect2.closeOnSelect = false;
),它将像你期望的。
要在添加值后保持弹出窗口打开,您应该编写一个类似于 github issue 中建议的处理程序
关于javascript - 按 Enter 键创建标签,但将文本保留在 Select2 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48096401/