有没有办法从Selectize中删除项目?
这是我的一个示例列表:
- AMNT
- QTY
- NA
当我传递 NA
的时候,它应该删除特定的项目:
$.fn.removeSelectorValue = function (value) {
var selectize = this[0].selectize;
selectize.removeItem(value);
return this;
};
这个不起作用,有人可以帮我吗?
有没有办法从Selectize中删除项目?
这是我的一个示例列表:
当我传递 NA
的时候,它应该删除特定的项目:
$.fn.removeSelectorValue = function (value) {
var selectize = this[0].selectize;
selectize.removeItem(value);
return this;
};
这个不起作用,有人可以帮我吗?
removeItem
移除给定值标识的 已选中 项目。若要从列表中移除选项,则应使用 removeOption
示例 - 打开http://brianreavis.github.io/selectize.js/,打开控制台并输入:
$('#select-beast')[0].selectize.removeOption(1)
移除Chuck Tesla作为可选项
$(document).on('click', 'div.selectize-input div.item', function(e) {
var select = $('#services').selectize();
var selectSizeControl = select[0].selectize;
// 1. Get the value
var selectedValue = $(this).attr("data-value");
// 2. Remove the option
select[0].selectize.removeItem(selectedValue);
select[0].selectize.refreshItems();
select[0].selectize.refreshOptions();
});
这段代码并没有将选项从下拉列表中删除,只是将其从所选选项中删除。
虽然我来晚了,但其他方法对我没有用,我不知道是因为我从远程源拉取了一个列表?
简而言之,有两个步骤:
你显然可以让这段代码更小,但我留下了详细的阅读性
var $select = $('#users').selectize();
var selectSizeControl = $select[0].selectize;
// 1. Get the value
var selectedValue = selectSizeControl.getValue()
// 2. Remove the option
selectSizeControl.removeOption( selectedValue )
onItemRemove
函数中查找保存的项目长度,如果length == 0
,则使用jQuery修复输入 - $('.selectize-input').css({'height':'35px'});