【发布时间】:2014-07-10 01:51:39
【问题描述】:
我想知道,如何在 Selectize.js 输入中获取当前选定项目的值?我检查了文档并搜索了与 Stackoverflow 相关的所有 selectize.js,但没有发现任何我可以使用的示例。有任何想法吗?这是我根据文档认为可行的方法,但给了我Undefined is not a function 错误。
请注意代码的最底部,我使用select.on('change';这(除了其他 API 方法)是我尝试过的。 on 更改效果很好,但不幸的是没有其他功能。
var select = $('#searchTextbox').selectize({
maxItems: 1, //Max items selectable in the textbox
maxOptions: 30, //Max options to render at once in the dropdown
searchField: ['text'], //Fields the autocomplete can search through. Example of another searchable field could be 'value'
openOnFocus: true,
highlight: true,
scrollDuration: 60, //currently does nothing; should say how many MS the dropdown and drop up animations take
create: false, //Whether or not the user is allowed to create fields
selectOnTab: true,
render: {
option: function(data, escape) {
var valueArray = [];
valueArray[0] = data.value.split(",");
var color = valueArray[0][0] == "1" ? "green" : "red";
return '<div class="option" style="color: '
+ color
+ ';">'
+ escape(data.text)
+ '</div>';
},
item: function(data, escape) {
var valueArray = [];
valueArray[0] = data.value.split(",");
var color = valueArray[0][0] == "1" ? "green" : "red";
return '<div class="option" style="color: '
+ color
+ ';">'
+ escape(data.text)
+ '</div>';
}
}
});
select.on('change', function() {
var test = select.getItem(0);
alert(test.val());
});
【问题讨论】:
-
要获得价值和文字,请在此处查看我的答案:stackoverflow.com/a/34718000/779238
标签: javascript jquery jquery-plugins selectize.js