i got a search bar with good suggestions and i need to be able to use the arrow keys to move through the options, but it doesn't work and i am really confused, please can you help me out, i have been battling with this stuff for days now.
var searchIndex = ["404 Error", "Address Bar", "Ajax", "Apache", "Autoresponder", "BitTorrent", "Blog", "Bookmark", "Bot", "Broadband", "Captcha", "Certificate", "Client", "Cloud", "Cloud Computing", "CMS", "Cookie", "CSS", "Cyberspace", "Denial of Service", "DHCP", "Dial-up", "DNS Record", "Domain Name", "Download", "E-mail", "Facebook", "FiOS", "Firewall", "FTP", "Gateway", "Google", "Google Drive", "Gopher", "Hashtag", "Hit", "Home Page", "HTML", "HTTP", "HTTPS", "Hyperlink", "Hypertext", "ICANN", "Inbox", "Internet", "InterNIC", "IP", "IP Address", "IPv4", "IPv6", "IRC", "iSCSI", "ISDN", "ISP", "JavaScript", "jQuery", "Meta Search Engine", "Meta Tag", "Minisite", "Mirror", "Name Server", "Packet", "Page View", "Payload", "Phishing", "POP3", "Protocol", "Scraping", "Search Engine", "Social Networking", "Socket", "Spam", "Spider", "Spoofing", "SSH", "SSL", "Static Website", "Twitter", "XHTML"];
var input = document.getElementById("searchBox"),
ul = document.getElementById("searchResults"),
inputTerms, termsArray, prefix, terms, results, sortedResults;
var search = function() {
inputTerms = input.value.toLowerCase();
results = [];
termsArray = inputTerms.split(' ');
prefix = termsArray.length === 1 ? '' : termsArray.slice(0, -1).join(' ') + ' ';
terms = termsArray[termsArray.length - 1].toLowerCase();
for (var i = 0; i < searchIndex.length; i++) {
var a = searchIndex[i].toLowerCase(),
t = a.indexOf(terms);
if (t > -1) {
results.push(a);
}
}
evaluateResults();
};
var evaluateResults = function() {
if (results.length > 0 && inputTerms.length > 0 && terms.length !== 0) {
sortedResults = results.sort(sortResults);
appendResults();
} else if (inputTerms.length > 0 && terms.length !== 0) {
ul.innerHTML = '<li>Whoah! <strong>' +
inputTerms +
'</strong> is not in the index. <br><small><a href="=' +
encodeURIComponent(inputTerms) + '">Try Google?</a></small></li>';
} else if (inputTerms.length !== 0 && terms.length === 0) {
return;
} else {
clearResults();
}
};
var sortResults = function(a, b) {
if (a.indexOf(terms) < b.indexOf(terms)) return -1;
if (a.indexOf(terms) > b.indexOf(terms)) return 1;
return 0;
}
var appendResults = function() {
clearResults();
for (var i = 0; i < sortedResults.length && i < 5; i++) {
var li = document.createElement("li"),
result = prefix +
sortedResults[i].toLowerCase().replace(terms, '<strong>'+ terms + '</strong>');
li.innerHTML = result;
ul.appendChild(li);
}
$('li').click(function(e) {
$('input').val($(this).text());
});
if (ul.className !== "term-list") {
ul.className = "term-list";
}
};
var clearResults = function() {
ul.className = "term-list hidden";
ul.innerHTML = '';
};
input.addEventListener("keyup", search, false);
var li = $('li');
var liSelected;
$(window).keydown(function(e) {
if (e.which === 40) {
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.next();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = li.eq(0).addClass('selected');
}
} else {
liSelected = li.eq(0).addClass('selected');
}
} else if (e.which === 38) {
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.prev();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = li.last().addClass('selected');
}
} else {
liSelected = li.last().addClass('selected');
}
}
});
.search-field,
.term-list {
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
body {
text-align: center;
background: #f2f2f2;
}
.title {
width: 100%;
margin: 3em 0 1em;
text-align: center;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-size: 170%;
font-weight: 400;
color: #2a5ba3;
text-shadow: #fff 1px 1px 0px, #ddd 2px 2px, #ddd 3px 3px 1px;
}
.search-field {
display: block;
width: 30%;
margin: 1em auto 0;
padding: 0.5em 10px;
border: 1px solid #999;
font-size: 130%;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-weight: 400;
color: #3e8ce0;
}
.term-list {
list-style: none inside;
width: 30%;
margin: 0 auto 2em;
padding: 5px 10px 0;
text-align: left;
color: #777;
background: #fff;
border: 1px solid #ddd;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-weight: 400;
}
.term-list li {
padding: 0.5em 0;
border-bottom: 1px solid #eee;
}
li.selected {
background: yellow
}
.term-list strong {
color: #444;
font-weight: 700;
}
.hidden {
display: none;
}
<script src=".1.1/jquery.min.js"></script>
<h1 class="title">AutoComplete Me</h1>
<input type="text" id="searchBox" class="search-field" autoFocus />
<ul id="searchResults" class="term-list hidden"></ul>
i got a search bar with good suggestions and i need to be able to use the arrow keys to move through the options, but it doesn't work and i am really confused, please can you help me out, i have been battling with this stuff for days now.
var searchIndex = ["404 Error", "Address Bar", "Ajax", "Apache", "Autoresponder", "BitTorrent", "Blog", "Bookmark", "Bot", "Broadband", "Captcha", "Certificate", "Client", "Cloud", "Cloud Computing", "CMS", "Cookie", "CSS", "Cyberspace", "Denial of Service", "DHCP", "Dial-up", "DNS Record", "Domain Name", "Download", "E-mail", "Facebook", "FiOS", "Firewall", "FTP", "Gateway", "Google", "Google Drive", "Gopher", "Hashtag", "Hit", "Home Page", "HTML", "HTTP", "HTTPS", "Hyperlink", "Hypertext", "ICANN", "Inbox", "Internet", "InterNIC", "IP", "IP Address", "IPv4", "IPv6", "IRC", "iSCSI", "ISDN", "ISP", "JavaScript", "jQuery", "Meta Search Engine", "Meta Tag", "Minisite", "Mirror", "Name Server", "Packet", "Page View", "Payload", "Phishing", "POP3", "Protocol", "Scraping", "Search Engine", "Social Networking", "Socket", "Spam", "Spider", "Spoofing", "SSH", "SSL", "Static Website", "Twitter", "XHTML"];
var input = document.getElementById("searchBox"),
ul = document.getElementById("searchResults"),
inputTerms, termsArray, prefix, terms, results, sortedResults;
var search = function() {
inputTerms = input.value.toLowerCase();
results = [];
termsArray = inputTerms.split(' ');
prefix = termsArray.length === 1 ? '' : termsArray.slice(0, -1).join(' ') + ' ';
terms = termsArray[termsArray.length - 1].toLowerCase();
for (var i = 0; i < searchIndex.length; i++) {
var a = searchIndex[i].toLowerCase(),
t = a.indexOf(terms);
if (t > -1) {
results.push(a);
}
}
evaluateResults();
};
var evaluateResults = function() {
if (results.length > 0 && inputTerms.length > 0 && terms.length !== 0) {
sortedResults = results.sort(sortResults);
appendResults();
} else if (inputTerms.length > 0 && terms.length !== 0) {
ul.innerHTML = '<li>Whoah! <strong>' +
inputTerms +
'</strong> is not in the index. <br><small><a href="http://google./search?q=' +
encodeURIComponent(inputTerms) + '">Try Google?</a></small></li>';
} else if (inputTerms.length !== 0 && terms.length === 0) {
return;
} else {
clearResults();
}
};
var sortResults = function(a, b) {
if (a.indexOf(terms) < b.indexOf(terms)) return -1;
if (a.indexOf(terms) > b.indexOf(terms)) return 1;
return 0;
}
var appendResults = function() {
clearResults();
for (var i = 0; i < sortedResults.length && i < 5; i++) {
var li = document.createElement("li"),
result = prefix +
sortedResults[i].toLowerCase().replace(terms, '<strong>'+ terms + '</strong>');
li.innerHTML = result;
ul.appendChild(li);
}
$('li').click(function(e) {
$('input').val($(this).text());
});
if (ul.className !== "term-list") {
ul.className = "term-list";
}
};
var clearResults = function() {
ul.className = "term-list hidden";
ul.innerHTML = '';
};
input.addEventListener("keyup", search, false);
var li = $('li');
var liSelected;
$(window).keydown(function(e) {
if (e.which === 40) {
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.next();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = li.eq(0).addClass('selected');
}
} else {
liSelected = li.eq(0).addClass('selected');
}
} else if (e.which === 38) {
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.prev();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = li.last().addClass('selected');
}
} else {
liSelected = li.last().addClass('selected');
}
}
});
.search-field,
.term-list {
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
body {
text-align: center;
background: #f2f2f2;
}
.title {
width: 100%;
margin: 3em 0 1em;
text-align: center;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-size: 170%;
font-weight: 400;
color: #2a5ba3;
text-shadow: #fff 1px 1px 0px, #ddd 2px 2px, #ddd 3px 3px 1px;
}
.search-field {
display: block;
width: 30%;
margin: 1em auto 0;
padding: 0.5em 10px;
border: 1px solid #999;
font-size: 130%;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-weight: 400;
color: #3e8ce0;
}
.term-list {
list-style: none inside;
width: 30%;
margin: 0 auto 2em;
padding: 5px 10px 0;
text-align: left;
color: #777;
background: #fff;
border: 1px solid #ddd;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-weight: 400;
}
.term-list li {
padding: 0.5em 0;
border-bottom: 1px solid #eee;
}
li.selected {
background: yellow
}
.term-list strong {
color: #444;
font-weight: 700;
}
.hidden {
display: none;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 class="title">AutoComplete Me</h1>
<input type="text" id="searchBox" class="search-field" autoFocus />
<ul id="searchResults" class="term-list hidden"></ul>
Share
Improve this question
edited May 24, 2017 at 6:55
Nadir Laskar
4,1702 gold badges19 silver badges33 bronze badges
asked May 24, 2017 at 6:48
Opeyemi OdedeyiOpeyemi Odedeyi
8001 gold badge20 silver badges48 bronze badges
6
- Try to use a dropdown instead of using lists – Venky Commented May 24, 2017 at 6:57
- Matt west's blog blog.teamtreehouse./… might help you codepen.io/matt-west/full/jKnzG – Venky Commented May 24, 2017 at 6:59
- 1 i have tried the drop down, it doesn't work. the search up there is perfect, i just need to be able to use the arrow keys to select too and it would be perfect. – Opeyemi Odedeyi Commented May 24, 2017 at 7:05
- I have narrowed your problem I believe. Continue with this if it helps jsfiddle/khkfe2ua – Venky Commented May 24, 2017 at 7:23
- 1 @venkey yeah the jsfiddle did it, but the reason i really wanted to use the one i sent is because it has multi-value suggest. if you check it out the one i sent, you would see that after typing a word, if you try typing another word it would still suggest and a lot of search bars don't have that. if you know of any search like that, please can you share it? – Opeyemi Odedeyi Commented May 24, 2017 at 7:39
1 Answer
Reset to default 5The only place your are getting reference to li
is here
var li = $('li');
var liSelected;
$(window).keydown(function(e) { ...
but at that time there are no li added in dom, hence $('li')
is returning zero elements.
Further,
You are losing your reference to li tags on every search / keypress as you are removing the li from the dom and adding new li's on every keypress after search.
Here,
var clearResults = function() {
debugger;
ul.className = "term-list hidden";
ul.innerHTML = ''; // Removing all li inside the ul
}
Thus, your old li
variable is referencing old stale li's which are now removed.
Solution:
You need to update the li
variable after adding new li inside the appendResults function.
var appendResults = function() {
clearResults();
for (var i = 0; i < sortedResults.length && i < 5; i++) {
var _li = document.createElement("li"),
result = prefix +
sortedResults[i].toLowerCase().replace(terms, '<strong>'+ terms + '</strong>');
_li.innerHTML = result;
ul.appendChild(_li);
}
li = $('li');
liSelected = li.filter('.selected');
$('li').click(function(e) {
$('input').val($(this).text());
});
if (ul.className !== "term-list") {
ul.className = "term-list";
}
};
Also your search function is getting called every keyup also for down and up keys you have to check for up and down key and return.
var search = function(e) {
if(e.which==40||e.which==38) return;
debugger;
inputTerms = input.value.toLowerCase();
results = [];
termsArray = inputTerms.split(' ');
prefix = termsArray.length === 1 ? '' : termsArray.slice(0, -1).join(' ') + ' ';
terms = termsArray[termsArray.length - 1].toLowerCase();
for (var i = 0; i < searchIndex.length; i++) {
var a = searchIndex[i].toLowerCase(),
t = a.indexOf(terms);
if (t > -1) {
results.push(a);
}
}
evaluateResults();
};
SNIPPET
var searchIndex = ["404 Error", "Address Bar", "Ajax", "Apache", "Autoresponder", "BitTorrent", "Blog", "Bookmark", "Bot", "Broadband", "Captcha", "Certificate", "Client", "Cloud", "Cloud Computing", "CMS", "Cookie", "CSS", "Cyberspace", "Denial of Service", "DHCP", "Dial-up", "DNS Record", "Domain Name", "Download", "E-mail", "Facebook", "FiOS", "Firewall", "FTP", "Gateway", "Google", "Google Drive", "Gopher", "Hashtag", "Hit", "Home Page", "HTML", "HTTP", "HTTPS", "Hyperlink", "Hypertext", "ICANN", "Inbox", "Internet", "InterNIC", "IP", "IP Address", "IPv4", "IPv6", "IRC", "iSCSI", "ISDN", "ISP", "JavaScript", "jQuery", "Meta Search Engine", "Meta Tag", "Minisite", "Mirror", "Name Server", "Packet", "Page View", "Payload", "Phishing", "POP3", "Protocol", "Scraping", "Search Engine", "Social Networking", "Socket", "Spam", "Spider", "Spoofing", "SSH", "SSL", "Static Website", "Twitter", "XHTML"];
var input = document.getElementById("searchBox"),
ul = document.getElementById("searchResults"),
inputTerms, termsArray, prefix, terms, results, sortedResults;
var search = function(e) {
if(e.which==40||e.which==38) return;
inputTerms = input.value.toLowerCase();
results = [];
termsArray = inputTerms.split(' ');
prefix = termsArray.length === 1 ? '' : termsArray.slice(0, -1).join(' ') + ' ';
terms = termsArray[termsArray.length - 1].toLowerCase();
for (var i = 0; i < searchIndex.length; i++) {
var a = searchIndex[i].toLowerCase(),
t = a.indexOf(terms);
if (t > -1) {
results.push(a);
}
}
evaluateResults();
};
var evaluateResults = function() {
if (results.length > 0 && inputTerms.length > 0 && terms.length !== 0) {
sortedResults = results.sort(sortResults);
appendResults();
} else if (inputTerms.length > 0 && terms.length !== 0) {
ul.innerHTML = '<li>Whoah! <strong>' +
inputTerms +
'</strong> is not in the index. <br><small><a href="http://google./search?q=' +
encodeURIComponent(inputTerms) + '">Try Google?</a></small></li>';
} else if (inputTerms.length !== 0 && terms.length === 0) {
return;
} else {
clearResults();
}
};
var sortResults = function(a, b) {
if (a.indexOf(terms) < b.indexOf(terms)) return -1;
if (a.indexOf(terms) > b.indexOf(terms)) return 1;
return 0;
}
var appendResults = function() {
clearResults();
debugger;
for (var i = 0; i < sortedResults.length && i < 5; i++) {
var _li = document.createElement("li"),
result = prefix +
sortedResults[i].toLowerCase().replace(terms, '<strong>'+ terms + '</strong>');
_li.innerHTML = result;
ul.appendChild(_li);
}
li = $('li'); // Update here
liSelected = li.filter('.selected'); // Update here
$('li').click(function(e) {
$('input').val($(this).text());
});
if (ul.className !== "term-list") {
ul.className = "term-list";
}
};
var clearResults = function() {
ul.className = "term-list hidden";
ul.innerHTML = '';
};
input.addEventListener("keyup", search, false);
var li = $('li');
var liSelected;
$(window).keydown(function(e) {
if (e.which === 40) {
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.next();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = li.eq(0).addClass('selected');
}
} else {
liSelected = li.eq(0).addClass('selected');
}
liSelected.trigger('click');
} else if (e.which === 38) {
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.prev();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = li.last().addClass('selected');
}
} else {
liSelected = li.last().addClass('selected');
}
liSelected.trigger('click');
}
});
.search-field,
.term-list {
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
body {
text-align: center;
background: #f2f2f2;
}
.title {
width: 100%;
margin: 3em 0 1em;
text-align: center;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-size: 170%;
font-weight: 400;
color: #2a5ba3;
text-shadow: #fff 1px 1px 0px, #ddd 2px 2px, #ddd 3px 3px 1px;
}
.search-field {
display: block;
width: 30%;
margin: 1em auto 0;
padding: 0.5em 10px;
border: 1px solid #999;
font-size: 130%;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-weight: 400;
color: #3e8ce0;
}
.term-list {
list-style: none inside;
width: 30%;
margin: 0 auto 2em;
padding: 5px 10px 0;
text-align: left;
color: #777;
background: #fff;
border: 1px solid #ddd;
font-family: "Arvo", "Helvetica Neue", Helvetica, arial, sans-serif;
font-weight: 400;
}
.term-list li {
padding: 0.5em 0;
border-bottom: 1px solid #eee;
}
li.selected {
background: yellow
}
.term-list strong {
color: #444;
font-weight: 700;
}
.hidden {
display: none;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 class="title">AutoComplete Me</h1>
<input type="text" id="searchBox" class="search-field" autoFocus />
<ul id="searchResults" class="term-list hidden"></ul>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745614836a4636170.html
评论列表(0条)