【发布时间】:2016-06-14 22:08:15
【问题描述】:
我正在尝试根据用户是否创建标签(应用红色 .btn-danger 样式)或者如果用户选择了现有标签(应用蓝色 .btn-primary 样式。
我尝试在 select 事件 (select2:select) 事件期间应用样式:
$("#ticker_select").on('select2:select', function(e) {
// If the e.params.data.id equals e.params.data.txt,
// then it is a user-created tag!
if (e.params.data.id == e.params.data.text) {
// User-created tag; Mark the tag red
$(".select2-selection__choice[title='" + e.params.data.text + "']").addClass('btn-danger');
}
else {
// User-selected tag; Mark the tag blue
$(".select2-selection__choice[title='" + e.params.data.text + "']").addClass('btn-success');
}
});
我看到样式已应用于标记,但一旦事件结束,select2 就会删除样式类引用。谁能告诉我如何将样式应用于标签而不选择2删除它们?非常感谢!
【问题讨论】:
标签: jquery-select2 jquery-select2-4