我正在使用 AJAX 資料填充Select2 。我的代碼如下所示。
function formatState(state) {
if (!state.id) { return state.text; }
var output = $('<span></span>'); // I would like get data `text[1]` of `processResults` here.
return output;
};
//more code here
cellEle.html(`<select multiple="multiple"></select>`);
let selectEle = cellEle.children("select").select2({
//more code here
ajax: {
//more code here
processResults: function (data) {
var options = [];
if (data) {
$.each(data, function (index, text) {
options.push({ id: index, text: text[0]});
});
}
return {
results: options,
more: false
};
},
},
templateSelection: formatState,
});
我想獲取內部的AJAX資料text[1]。processResultsformatState()templateSelection
我怎么才能得到它 ?
uj5u.com熱心網友回復:
您可以通過 傳遞任何額外的資料processResults。在你的情況下,它可以是
var options = [];
if (data) {
$.each(data.items, function (index, text) {
options.push({ id: index, text: text[0], text2: text[1] /* <-- extra data */});
});
}
return {
results: options,
more: false
};
然后訪問它 templateSelection
if (!state.id) {
return state.text;
}
return $(`<span>${state.text} & ${state.text2}</span>`);
http://jsfiddle.net/moshfeu/3hrupdn1/22/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/419777.html
標籤:
上一篇:如何將可捕獲的錯誤回傳到Struts2中的ajax呼叫?
下一篇:我正在嘗試發出AJAX獲取請求,但我不斷收到錯誤“UncaughtSyntaxError:Unexpectedtoken')'”
