HTMLでselect2に選択されたID /テキストをHTMLで表示および選択できるように設定する方法はありますか?Select2はHTMLで選択した値を設定します
$("#users").select2({
placeholder: "select user...",
escapeMarkup: function (markup) { return markup; },
ajax: {
url: "users.json",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (response, page) {
return {
results: response.data
};
},
cache: true
},
templateResult: function(item){
if (item.loading) {
return item.text;
}
return item.username + " <small><i>(" + item.role + ")</i></small> "; // display data in html
},
templateSelection: function(item){
if (item.username){
return item.username + " <small><i>(" + item.role + ")</i></small> "; // select row in html
}
return item.text; // for placeholder
}
});
私が選択した値に設定するには::私はjqueryのSELECT2バージョン4.0.0
これはコードで使用しています
// bring data for to set selected value, data contains fields data
var selected_user_id = data.id,
// this is the content that I need to use as it is HTML content
selected_user_text_html = data.username + " <small><i>(" + data.role + ")</i></small> ",
// this is the one that I am using and is not html because there is no way to put html inside a option select item
selected_user_text_regular = data.username + " (" + data.role + ") ";
// set selected value
$("#users").append("<option value='"+selected_user_id+"' selected='selected'>"+selected_user_text_regular+"</option>");
// trigger update
$("#users").trigger("change");
は、私が設定できる方法はありますプレーンテキストの代わりにHTMLで選択したテキスト?
を交換するために以下の点を考慮更新し、私はそれは本当にハードに何をしようとする動作するように発見しています。 「選択した値を設定するには:」コードはどこにありますか。あなたはおそらくhtmlを含めるべきです。 – Seabizkit