【发布时间】:2018-01-15 10:02:35
【问题描述】:
我的问题,当用户在输入字段上搜索文本时,如何选择第一个过滤的匹配列表项?如果用户点击进入它会自动点击第一个选定的项目。我将非常感谢您的帮助。提前致谢。
//pug file which displays dynamic list items. Each list item has href tag url link.
.row
.col-md-8.offset-md-2.pad
.form(class="form-row justify-content-center deparrSearchForm")
.form-group.col-md-8
input(type="text" class="form-control form-control-lg" id="fromStation" name="fromStation" placeholder="Type train station name & press enter")
for prop in stationName
ul(class="list-group text-center" id="mylist")
a(href="/train/search-deparrtrain-submit/"+prop.stationShortCode+'/'+prop.stationName)
li(class="list-group-item") #{prop.stationName}
// Jquery which filter my input search text and display list items
$("#fromStation").on("keyup", function() {
var searchText = $(this).val().toLowerCase();
$("#mylist li").filter(function(){
var text= $(this).text().toLowerCase();
if(text.indexOf(searchText) >= 0){
$(this).show();
}
else{
$(this).hide();
}
});
});
【问题讨论】:
-
过滤部分可以使用
first()api.jquery.com/first -
我试过了,它不仅选择了第一个过滤的列表项。
-
你能用你的完整代码制作codepen、jsbin或类似的东西吗?
标签: javascript jquery pug