【发布时间】:2012-08-14 14:40:29
【问题描述】:
我正在尝试计算列表中的选项数量。但是,由于在输入框中输入了搜索文本,一些选项已被隐藏。
我开始研究 .size() 和 .length 但只得到了完整的列表,而不是那些没有隐藏的。为了简化,我将其更改为尝试找到隐藏的选项(我可以使用 :not later)。
$('#txtListSearch').keyup(function(evt) {
if($(this).val().length < 1) {
$('#selContactLists option').show();
} else {
$('#selContactLists option:not(:contains("' + $(this).val() + '"))').hide();
if($('#selContactLists').size()) {
$('#selContactLists option:contains("' + $(this).val() + '")').first().attr('selected', 'selected');
} else {
}
}
console.log($('#selContactLists option').filter(':hidden'));
});
我也试过:console.log($('#selContactLists option:hidden')); 我从来没有完全得到我期望的数字。谁能看出我哪里出错了?
更奇怪的是,如果我更改选择的“大小”以便默认显示多个项目,则在 chrome 上它永远不会隐藏任何选项。
【问题讨论】:
-
你也可以发布一个 HTML 示例吗?
-
附注
size()因为你提到了它。它已被标记为已弃用。来自文档:The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.(api.jquery.com/size) -
选项隐藏或显示的依据是什么?
-
由搜索框中的文字决定:jsfiddle.net/kcRUB/3
-
是的,我见过小提琴。我假设输入
t应该同时显示two和three?而o应该只显示one?