【发布时间】:2015-12-24 13:48:50
【问题描述】:
我正在尝试使用 Select2.js 4.0 版来创建标签选择。如果它不退出,我希望创建一个新标签,并且我希望使用'data-html' 标签格式化结果。
问题在于这两个元素(createTag 和 templateResult)独立工作,但是当我将它们一起使用时,当用户在搜索框中键入时,自动完成选项不再显示。
是我做错了什么,还是 Select2 的限制?
application.js
$("#taglist").select2({
tokenSeparators: [',', ' '],
tags: true,
createTag: function (tag) {
// Case insensitive search for tag
tagExists = false;
$("#taglist option").each(function() {
if ($.trim(tag.term).toUpperCase() == $.trim($(this).text()).toUpperCase()) {
tagExists = true;
}
});
// If the tag does not exist create it
if (!tagExists) {
return {
id: tag.term,
text: tag.term + " (create)",
isNew: true
};
}
},
templateResult: function (choice) {
if (!choice.id) { return choice.text; }
var $choice = $(
'<span><a>' + data.element.getAttribute('data-html') + '</a></span>'
);
return $choice;
}
});
【问题讨论】:
标签: javascript jquery html jquery-select2 jquery-select2-4