Javascript 按Enter键创建标记,但将文本保留在Select2中
选择2版本:4.0.5 问题: 在多任务处理中(带标记:true或带预填充标记),每当我按enter键选择标记时,它都会创建标记,但会将文本保留在新标记的右侧,如果我在之后按另一个键,它就会消失。(仅在使用Enter键时发生) 示例:Javascript 按Enter键创建标记,但将文本保留在Select2中,javascript,jquery,html,jquery-select2,Javascript,Jquery,Html,Jquery Select2,选择2版本:4.0.5 问题: 在多任务处理中(带标记:true或带预填充标记),每当我按enter键选择标记时,它都会创建标记,但会将文本保留在新标记的右侧,如果我在之后按另一个键,它就会消失。(仅在使用Enter键时发生) 示例: 如您所见,当我键入一个标记并按Enter键时,它会创建标记(预期行为),但它会将原始文本保留在新标记的右侧 这是正常的行为吗?或者我该怎么办 我的选择2代码: var editorTagElement = document.createElement(&quo
如您所见,当我键入一个标记并按Enter键时,它会创建标记(预期行为),但它会将原始文本保留在新标记的右侧 这是正常的行为吗?或者我该怎么办 我的选择2代码:
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);
var editorTagElement=document.createElement(“选择”);
类名称('myEditor');
setAttribute(“多个”、“多个”);
editorTagElement.style.overflow='auto';
editorTagElement.style.fontSize='13px';
this.options select2={};
this.options选择2.placeholder=“”;
this.options选择2.language=ReturnCustomLanguage();
this.options选择2.formatNoMatches=函数(){
返回“”;
};
this.options select2.allowClear=true;
this.options选择2.minimumInputLength=2;
this.options select2.tags=true;
this.options选择2.tokenSeparators=[''];
this.options select2.createTag=函数(参数){
变量项=$.trim(参数项);
如果(术语长度<2){
返回null;
}
term=term.replace(//g,“”);
返回{
id:术语,
案文:任期,
纽塔格:是的
}
};
this.options select2.closeOnSelect=false;
$('.myEditor').select2(this.options select2);
在此之后,我将编辑器附加到正文或html中的另一个位置…选项
closeOnSelect
将保留该值,而这不是预期的行为。
如果将closeOnSelect
更改为true
(默认值,只需删除代码中的this.options select2.closeOnSelect=false;
),它将按照您的预期工作
要在添加值后保持弹出窗口的打开状态,您应该编写一个类似于中建议的处理程序,我猜它是带有
createTag
函数的。尝试在返回之前清除。如果你能创造一个片段,我会尽力帮你更多…是的,就是这样。。。所以,如果我想打开弹出窗口来写更多的标签,它会一直保存单词直到我再次写,如果我每次都关闭弹出窗口,它会工作得很好。。。看起来像个虫子。但是谢谢!