您只能使用filter
选项选择一些项目。更改完成后,您需要遍历所有元素并根据需要更新过滤器。
样品的实现可能是这样的(见现场演示here):
$(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();
});
感谢花花公子。我明白你的意思与过滤器选项只适用于一些禁用。例如,当我在第一个,第二个中点击4时,我仍然可以在第三个中点击2。我还是比较新的JavaScript/jQuery的,所以我会研究你的示例,看看语言过程。我想要做的是有一个输出元素,就像我猜的购物车,从而显示每个列表中的选择,并且每个数字只能有一个代表。再次感谢。 – user1648449