gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:49:46 26 4
gpt4 key购买 nike

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

我正在使用 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/

最佳答案

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

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

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

关于javascript - 使用 Ajax 源设置 select2 元素初始值的简单示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com