您只能使用该filter
选项使某些项目可选。进行更改后,您需要遍历所有元素并根据需要更新过滤器。
示例实现可能如下所示(请参阅此处的现场演示):
$(document).ready(function() {
function enableAll() {
$('li').addClass('enabled');
}
function disableSelected(selected) {
// iterate thru all list items
$.each($('li'), function(i, item) {
// if the text is selected somewhere..
if ($.inArray($(this).text().toLowerCase(), selected) >= 0) {
// ..and it's not here
if (!$(this).hasClass('ui-selected')) {
// remove
$(this).removeClass('enabled');
}
}
});
}
$('ul').selectable({
// only matching items will be selectable
filter: '.enabled',
// change handler
stop: function(event, ui) {
// we will collect selected texts in this variable
var selectedTexts = new Array();
// go thru all selected items (not only in current list)
$('.ui-selected').each(function() {
// extract selected text
var text = $(this).text().toLowerCase();
// add to array if not already there
if ($.inArray(text, selectedTexts) < 0) {
selectedTexts.push(text);
}
});
// enable all list items for selectable
enableAll();
// disable list items with text that is already selected
disableSelected(selectedTexts);
}
});
// initialization
enableAll();
});