2017-12-21 11 views
0

Multiselectを使用しているSelect2という名前のjQueryプラグインを使用しています。以前は私の別のウェブサイトにも同じプラグインを使用していましたが、今はかなり古いです。私はドロップダウンからオプションを選択するときに と表示されますClick to see image このoptgroupタグは値が選択されたものにしたいと思っています。以前JsFiddlejQueryでoptgroupのラベル+値を表示するにはselect2

私は値でOPTGROUP表示するには、このスクリプトを使用していた:ここ はselect2のワーキングデモです

function format(item) { 
    opt = $(item.element); 
    sel = opt.text(); 
    og = opt.closest('optgroup').attr('label'); 
    return og+' | '+item.text; 
} 
$("select").select2({ 
    formatSelection: format, 
    escapeMarkup: function(m) { return m; } 
}); 
$(".select2_sample3").select2({ 
    tags: [] 
}); 

答えて

1

使用templateSelection

そして、これはあなたのコードに基づいた例を取り組んで

function formatState (item) { 
    opt = $(item.element); 
    og = opt.closest('optgroup').attr('label'); 
    return og+' | '+item.text; 
}; 

$('#example').select2({ 
    placeholder: 'Select a Category', 
    templateSelection: formatState 
}); 

jsfiddle:https://jsfiddle.net/synz/t2zqjdf0/

リソース:https://select2.org/selections#templating

+0

はい、あなたは完全に私のために働いています。ありがとうございました。 – GermanCoder

関連する問題