使用Ajax源设置select2元素初始值的简单示例?

时间:2015-06-08 14:36:21

标签: javascript jquery ajax jquery-select2 jquery-select2-4

我正在尝试设置连接到Ajax数据源的Select2元素的初始值。

我正在使用Select2的v4,并使用the docs,它解释了initSelection已被替换。

我已经开始尝试调整the example in the "Loading remote data" section of the Select2 documentation。这是我设置初始值的代码:

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/

1 个答案:

答案 0 :(得分:2)

错误是您的templateResulttemplateSelection功能。 'repo'参数是一个不包含'name'和'full_name'属性的对象,你应该使用'text'属性。

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

在这里工作JSFiddle:http://jsfiddle.net/45891w6v/1/

相关问题