Select2在初始化期间恢复颜色

时间:2018-09-18 08:18:10

标签: jquery jquery-select2

我正在使用Select2,并且从服务器获得了一些价值。当用户更改值时,Select2会以这种方式更改文本的颜色:

 $("#idIuSelectCompany").select2({
        cache: true,
        tags: true,
        placeholder: "Inserire il nome dell'azienda",
        tokenSeparators: [','],
        ajax: {
            url: './../../back-end/switch-ajax-listening/switch-ajax-listening.php',
            type: "post",
            dataType: 'json',
            delay: 250,
            data: function (params)
            {
                return {
                    searchTerm: params.term, // search term
                    actionId: "getSelector",
                    jsonField: "idSelectCompany"
                };
            },
            processResults: function (response)
            {
                return {
                    results: response,
                    id: response.term,
                    text: response.term + " (new)",
                    newOption: true
                };
            },
        },
        createTag: function (params)
        {
            var term = $.trim(params.term);


            if (term === "-")
            {
                return {
                    id: term,
                    text: term,
                };
            }


            if (term === '')
            {
                return null;
            }
            //console.log(term.length);
            if (term.length > 50)
            {
                iuAlertError("The name company must be less than 50 char");
                return null;
            }
            return {
                id: term,
                text: term + ' (new)'
            };
        },
    }).on('change select2:close', function (e)
    {
        if(pageUpdate===true)
        {
            $(this).next('.select2-container').find('.select2-selection__rendered').text(function (idx, txt)
            {
                if (iuInputCompany.localeCompare(txt) !== 0)
                {
                    $(this).addClass('iuBlueColor');
                    return txt;
                }
                else
                {
                    $(this).removeClass('iuBlueColor');
                }
            })
        }
    });

但是我必须在运行时附加新的Select2或删除它。 为此,在执行这些操作(添加或删除)之后,我调用以下指令:$('select').select2();以初始化Select2,但是我丢失了文本的颜色。您是否有一些想法可以还原颜色?

0 个答案:

没有答案