我在使用 ajax 调用设置方法 initSelection 时遇到问题,我返回“未定义”。我检查并ajax调用返回正确的结果..我不明白如何设置回调方法以确保您选择'预设值。
function mediaFormatResult(media) {
var markup = "<div class='media-title'>" + media.name + ", " + media.prov + " (" + media.region + ")</div>";
return markup;
}
function mediaFormatSelection(media) {
return media.name +", " + media.prov + " (" + media.region + ")";
}
$field = $('#comune');
$($field).select2({
placeholder: "Seleziona il tuo comune",
minimumInputLength: 3,
initSelection: function(element, callback) {
return $.ajax({
type: "POST",
url: "myurl",
dataType: 'json',
data: { id: (element.val())},
success: function(data){
//results: data.results;
}
}).done(function(data) {
//console.log(data);
callback(data);
});
},
ajax: {
quietMillis: 100,
url: "myurl",
dataType: 'json',
type: 'POST',
data: function (term, page) {
return {
q: term,
page_limit: 10
};
},
results: function (data, page) {
return {results: data.results};
}
},
formatResult: mediaFormatResult,
formatSelection: mediaFormatSelection,
formatNoMatches: function () { return "Nessun risultato trovato!";},
formatSearching: function () { return "Ricerco.."; },
formatInputTooShort: function(input, min) {return "Inserire "+ (min - input.length) + " caratteri.";},
dropdownCssClass: "bigdrop",
});
有什么不对吗?