简体   繁体   中英

I use select2 formatSelection ,but a style error has occured

var list = e.data.goods_list;

$.each(list, function(k, v) {
    var str = '';
    str = str + '<optgroup label="'+v[0].store_name+'">';
    $.each(v, function(kk, vv) {
        str = str + '<option value="'+vv.goods_id+'">'+vv.goods_name+'</option>';
    });
    str = str + '</optgroup>';
    $('#goodsSelect2').append(str);
});

$('#goodsSelect2').select2({formatSelection:test});
console.log(@goodsIds);

var goodsIdsStr = "@goodsIds";
if(goodsIdsStr !=null && goodsIdsStr != "" && goodsIdsStr != undefined){
    var arr = goodsIdsStr.split(",");
    console.log(arr);
    $("#goodsSelect2").val(arr).trigger('change');
}

Above is part of my code. When I use $('#goodsSelect2').select2(); , all is ok 在此处输入图片说明

However when I use $('#goodsSelect2').select2({formatSelection:test}) , the function "test" is useful but the style is wrong. 在此处输入图片说明

formatSelection必须返回e.text,否则呈现错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM