[英]jQuery autocomplete - modify divs based on autocomplete results (e.g. show only divs with matching results)
I'm looking for a way to show/hide divs based on the jQuery autocomplete search results. 我正在寻找一种基于jQuery自动完成搜索结果显示/隐藏div的方法。
This is the autocomplete function I have: 这是我拥有的自动完成功能:
$('#catalogSearch .searchBox').mouseup(function (e) { e.preventDefault(); }).autocomplete({
source: services,
autoFill: true,
minChars: 0,
focus: function (event, ui) {
event.preventDefault(); // Prevent ui.item.value from showing up
$('#catalogSearch .searchBox').val(ui.item.label);
return false;
},
select: function (event, ui) { // Triggered when an item is selected from the menu?
$('#catalogSearch .searchBox').val(ui.item.label);
window.location.href = ui.item.value.find("a").attr("href");
return false;
},
open: function () { $('.ui-menu').css('max-width', '100%') }
})
The source 'services' is just an array of objects with label & value. 源“服务”只是带有标签和值的对象数组。 Basically, I want to filter out my divs based on the autocomplete results (the list that shows up on .ui-menu-item) as shown here: before & after 基本上,我想根据自动完成结果(显示在.ui-menu-item上的列表)过滤掉div,如下所示: 之前和之后
(In case if the images don't work) My html looks like this: (如果图像不起作用的话)我的html如下所示:
<div id="catalogSearch">
...
<input type="text" class="searchBox ui-autocomplete-input ...>
<input id="catalogSearchSubmit" class="searchSubmit" type="submit">
...
</div>
<div class="row">
...
<div id="product-apple">...</div>
<div id="product-banana">...</div>
<div id="product-cherry">...</div>
<div id="product-melon">...</div>
<div id="product-pineapple">...</div>
<div id="product-kiwi">...</div>
...
</div>
And basically, I want to use this filtering function (or something like this): 基本上,我想使用此过滤功能(或类似的东西):
var filterResults = function () {
var results = $('.ui-menu-item');
$('.product-list.item').hide();
results.each(function (r) {
$('#product-' + $(this).text()).show();
});
};
and show only relevant divs. 并仅显示相关的div。 For example, if I search "pl", I would only want to see div "apple", "pineapple" & "plum." 例如,如果我搜索“ pl”,则只希望看到div“苹果”,“菠萝”和“李子”。 I tried to add it as a part of the search event but seems it doesn't work. 我尝试将其添加为搜索事件的一部分,但似乎不起作用。 Is there any way I can do it within the autocomplete scope? 在自动完成范围内,有什么办法可以做到?
Thank you for your answer in advance! 预先感谢您的答复!
What I would do is something like this : 我要做的是这样的:
function search(value){
//$(elems) == your divs selector
$(elems).hide();
$(elems).each(function(){
if($(this).html().match(value)){
$(this).show();
}
});
}
You can get read rid of your divs id. 您可以摆脱div ID。
Then use this search function inside jq autocomplete focus and/or select and give it ui.item.label value, which might be the value you're looking for. 然后在jq自动完成焦点内使用此搜索功能和/或选择并为其提供ui.item.label值,该值可能是您要查找的值。
Note : As all your possible results are already loaded on page, I don't see the point of autocomp as you can get this search function to work instantly like this 注意:由于所有可能的结果都已经加载到页面上,因此我看不到autocomp的意义,因为您可以使此搜索功能像这样立即工作
$('.searchBox').keyup(function(){ search( $(this).val() ) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.