select2 json 将所有选项显示为 .text() 而不是选中
select2 json displaying all options as .text() instead of selected
http://www.codeply.com/go/i8A9BlgzGL
以上是代码示例。设置非常简单。它有一个 json 数据变量、一个输入框和一个 select2 框。当 select2 值更改时,它会根据事件将 select2 的 .text() 值放入输入中。
容易/快速注意到的是 json 中的所有可能选项都加载为 .text() 值。我一直找不到解决方法。如果我使用服务器 ajax 调用来加载数据,则此问题似乎不存在。
$('#outitem').select2({
placeholder: 'Find Item',
width: '90px',
allowClear: true,
SingleSelection: true,
data: data
});
$('#outitem').on('select2:select', function(){
$('#output').val($('#outitem').text());
});
您将从整个 <select>
元素中获取 text()
,包括所有选项。
更改为仅获取所选选项的文本
$('#outitem').on('select2:select', function(){
$('#output').val($('#outitem option:selected').text());
});
http://www.codeply.com/go/i8A9BlgzGL
以上是代码示例。设置非常简单。它有一个 json 数据变量、一个输入框和一个 select2 框。当 select2 值更改时,它会根据事件将 select2 的 .text() 值放入输入中。
容易/快速注意到的是 json 中的所有可能选项都加载为 .text() 值。我一直找不到解决方法。如果我使用服务器 ajax 调用来加载数据,则此问题似乎不存在。
$('#outitem').select2({
placeholder: 'Find Item',
width: '90px',
allowClear: true,
SingleSelection: true,
data: data
});
$('#outitem').on('select2:select', function(){
$('#output').val($('#outitem').text());
});
您将从整个 <select>
元素中获取 text()
,包括所有选项。
更改为仅获取所选选项的文本
$('#outitem').on('select2:select', function(){
$('#output').val($('#outitem option:selected').text());
});