在jQuery中,如何只需按键盘上的键就可以搜索未排序的列表?
我有一张ul li制作的国家列表,在表格的一列下面。我需要在键盘上按键时搜索国家在jQuery中,如何只需按键盘上的键就可以搜索未排序的列表?,jquery,html,jquery-ui,css,Jquery,Html,Jquery Ui,Css,我有一张ul li制作的国家列表,在表格的一列下面。我需要在键盘上按键时搜索国家 <ul class="country-search"> <li id="1">Country 1</li> <li id="2">Country 2</li> <li id="3">Country 3</li> <li id="4">Country 4</li> </ul>
<ul class="country-search">
<li id="1">Country 1</li>
<li id="2">Country 2</li>
<li id="3">Country 3</li>
<li id="4">Country 4</li>
</ul>
- 国家1
- 国家2
- 国家3
- 国家4
样品在这里
需要在按键上搜索项目,而不是在任何输入框上键入。有帮助吗 听起来您正在寻找的功能是内置在HTML中的
对象中的,假设您可以使用
您可以将size
属性设置为20
,它看起来就像您的列表一样。下面是一些例子
前
选中时,您在元素上放置什么类?此功能内置于HTML中的
对象中。您可以将size
属性设置为20,它看起来就像您的列表一样。您可以使用该标记而不是列表吗?这样您就不会用autocomplete()搜索输入或搜索选择[size]
?如果用户正在键入某些内容,则仍应与表单交互,并考虑接下来会发生什么?选择是否要用作表单栏的一部分?检索到更多数据?您希望如何处理以同一字母开头的多个国家?如果你要使用连续的按键,例如:“Aus”,你将如何让用户知道他们有什么类型,以及当用户输入错误时,你将如何让他们修复?你读过问题了吗?通过在html中再写两行代码,这并不像你想象的那么容易。@Codegiant检查我的更新,可能更接近你想要的内容。@Codegiant我调整了我的答案,让你获得确切的li
,而不是知道列表中存在一行。这感觉像是你要求的。
<select class="country-search" size="20">
<option id="1" value="Country 1">Country 1</option >
<option id="2" value="Country 2">Country 2</option >
<option id="3" value="Country 3">Country 3</option >
<option id="4" value="Country 4">Country 4</option >
<!-- ... -->
<option id="20" value="Country 20">Country 20</option >
<!-- ect. -->
</select>
$(":not(input)").keypress(function(event ){
/*Search the country for the letter typed*/
var NotFound = true;
if($("ul.country-search li").each(function() {
/* If you haven't found a country starting with the key pressed check if this one starts with the same key*/
if(NotFound && $( this ).text().indexOf(event.which) == 0)
{
NotFound = false; //Stop looking a country starts with this key
$(this).focus(); //Give this item focus
$("ul.country-search li").removeClass('highlightingClass'); //remove previous highlight
$(this).addclass('highlightingClass'); //Add highlight to current li
/*do something else*/
}
});
});