[英]Javascript Product Search (working, but need to filter by search term)
I have a little product search code that I've been working on for a while.我有一个小的产品搜索代码,我已经研究了一段时间。 It works great, although a bit backwards.
它工作得很好,虽然有点倒退。
The more keywords I type in, ideally, the less products will show up (because it narrows down the results).理想情况下,我输入的关键字越多,显示的产品就越少(因为它会缩小结果的范围)。 But as is stands, the more keywords I type in my search system, the MORE products are displayed, because it looks for any product with any of the keywords.
但就目前而言,我在搜索系统中输入的关键字越多,显示的产品就越多,因为它会查找带有任何关键字的任何产品。 I want to change the script so that it only shows results if they include ALL the searched for keywords, not ANY of them...
我想更改脚本,以便它只显示结果,如果它们包含所有搜索到的关键字,而不是其中的任何一个...
Sorry for the long-winded explanation.抱歉冗长的解释。
Here's the meat and potatoes (jsfiddle):这是肉和土豆(jsfiddle):
http://jsfiddle.net/yk0Lhneg/ http://jsfiddle.net/yk0Lhneg/
HTML: HTML:
<input type="text" id="edit_search" onkeyup="find_my_div();">
<input type="button" onClick="find_my_div();" value="Find">
<div id="product_0" class="name" style="display:none">Mac
<br/>Apple
<br/>
<br/>
</div>
<div id="product_1" class="name" style="display:none">PC
<br/>Windows
<br/>
<br/>
</div>
<div id="product_2" class="name" style="display:none">Hybrid
<br/>Mac PC Apple Windows
<br/>
<br/>
</div>
JAVASCRIPT:爪哇脚本:
function gid(a_id) {
return document.getElementById(a_id);
}
function close_all() {
for (i = 0; i < 999; i++) {
var o = gid("product_" + i);
if (o) {
o.style.display = "none";
}
}
}
function find_my_div() {
close_all();
var o_edit = gid("edit_search");
var str_needle = edit_search.value;
str_needle = str_needle.toUpperCase();
var searchStrings = str_needle.split(/\W/);
for (var i = 0, len = searchStrings.length; i < len; i++) {
var currentSearch = searchStrings[i].toUpperCase();
if (currentSearch !== "") {
nameDivs = document.getElementsByClassName("name");
for (var j = 0, divsLen = nameDivs.length; j < divsLen; j++) {
if (nameDivs[j].textContent.toUpperCase().indexOf(currentSearch) !== -1) {
nameDivs[j].style.display = "block";
}
}
}
}
}
So, when you search "mac pc" the only result that should be displayed is the hybrid, because it has both of those keywords.因此,当您搜索“mac pc”时,应该显示的唯一结果是混合,因为它同时包含这两个关键字。 Not all 3 products.
并非所有 3 种产品。
Thank you in advance!先感谢您!
I changed a little bit your code to adjust it better to my solution.我对您的代码进行了一些更改,以更好地适应我的解决方案。 I hope you don't mind.
我希望你不要介意。 You loop first over the terms, and then through the list of products, I do it the other way around.
你首先遍历条款,然后遍历产品列表,我反过来做。
How this solution works:此解决方案的工作原理:
function gid(a_id) { return document.getElementById(a_id); } function close_all() { for (i = 0; i < 999; i++) { var o = gid("product_" + i); if (o) { o.style.display = "none"; } } } function find_my_div() { close_all(); var o_edit = gid("edit_search"); var str_needle = edit_search.value; str_needle = str_needle.toUpperCase(); var searchStrings = str_needle.split(/\\W/); // I moved this loop outside var nameDivs = document.getElementsByClassName("name"); for (var j = 0, divsLen = nameDivs.length; j < divsLen; j++) { // set a counter to zero var num = 0; // I moved this loop inside for (var i = 0, len = searchStrings.length; i < len; i++) { var currentSearch = searchStrings[i].toUpperCase(); // only run the search if the text input is not empty (to avoid a blank) if (str_needle !== "") { // if the term is found, add 1 to the counter if (nameDivs[j].textContent.toUpperCase().indexOf(currentSearch) !== -1) { num++; } // display only if all the terms where found if (num == searchStrings.length) { nameDivs[j].style.display = "block"; } } } } }
<input type="text" id="edit_search" onkeyup="find_my_div();"> <input type="button" onClick="find_my_div();" value="Find"> <div id="product_0" class="name" style="display:none">Mac <br/>Apple <br/> <br/> </div> <div id="product_1" class="name" style="display:none">PC <br/>Windows <br/> <br/> </div> <div id="product_2" class="name" style="display:none">Hybrid <br/>Mac PC Apple Windows <br/> <br/> </div>
You can also see it on this version of your JSFiddle: http://jsfiddle.net/yk0Lhneg/1/你也可以在这个版本的 JSFiddle 上看到它: http : //jsfiddle.net/yk0Lhneg/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.