簡體   English   中英

使用Ajax源設置select2元素的初始值的簡單示例?

[英]Simple example to set initial value of select2 element with Ajax source?

我試圖設置連接到Ajax數據源的Select2元素的初始值。

我使用的是Select2 v4,並遵循docs ,該文檔解釋了initSelection已被替換。

我首先嘗試改編Select2文檔的“加載遠程數據”部分中的示例 這是我設置初始值的代碼:

var $gitElement = $('.select2');
var option = new Option("yosssi/ejr", '29424443', true, true);
$gitElement.append(option);
$gitElement.trigger('change');

我最后得到一個顯示undefined (undefined)的標簽。 我究竟做錯了什么?

JSFiddle在這里: http : //jsfiddle.net/t4Ltcm0f/4/

錯誤是您的templateResulttemplateSelection函數。 'repo'參數是一個不包含'name'和'full_name'屬性的對象,您應該使用'text'屬性。

function(repo) { 
    return repo.text || repo.name + ' (' + repo.full_name + ')';
}

在這里可以使用JSFiddle: http//jsfiddle.net/45891w6v/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM