[英]Is there a way for the jQuery plugin select2 to retain the class of each select option?
I'm using the jQuery plugin select2 and I'm wondering if there was any way that you could make it retain (merge) a class you have set on each option
element in the inital select
field and insert it as an additional class in the resulting li
tag? 我正在使用jQuery插件select2 ,我想知道是否有任何方法可以使其保留(合并)您在初始select
字段中的每个option
元素上设置的类,并将其作为附加类插入到产生的li
标签?
So currently they get a class of select2-results__option
; 因此,当前他们得到一类select2-results__option
; what I want would be something like: select2-results__option myclass
. 我想要的是这样的: select2-results__option myclass
。
Can this be done? 能做到吗?
Also, it doesn't have to be a class, even retaining a data
attribute would work too. 此外,它不必是一个类,甚至保留了data
属性将工作太。
As per the documentaiton for the select2 plugin, you can do this by overriding the function that creates the results https://select2.github.io/examples.html#tags 根据select2插件的文档说明,您可以通过覆盖创建结果的函数https://select2.github.io/examples.html#tags来执行此操作
function formatState (state) {
if (!state.id) { return state.text; }
var $state = $(
'<span><img src="vendor/images/flags/' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
);
return $state;
};
$(".js-example-templating").select2({
templateResult: formatState
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.