[英]Search by pressing the enter key on keyboard
Trying to also search by pressing enter key.也尝试按回车键进行搜索。 Works with the button but for some reason the code i have for the key press is not working.
与按钮一起工作,但由于某种原因,我的按键代码不起作用。 Javascript:
Javascript:
function displayMatches() {
const searchText = document.querySelector('.search').value;
const matchArray = findMatches(searchText, name);
const html = matchArray.map(place => {
const regex = new RegExp(searchText);
const nameName = place.name.replace(regex, `<span class="hl">${searchText}</span>`);
return `
<a href="${place.url}" target="_blank">
<li>
<span class="name">${nameName} <br> ${(place.price)}</span>
<img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100">
</li>
</a>
`;
}).join('');
suggestions.innerHTML = html;
}
const suggestions = document.querySelector('.suggestions');
const searchBtn = document.querySelector('.btn-search');
searchBtn.addEventListener('click', displayMatches);
var input = document.getElementById('.search');
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById('.btn-search').click();
}
});
In:在:
document.getElementById('.btn-search').click();
Are you sure .btn-search
is a id property?你确定
.btn-search
是一个 id 属性吗? Seems like a class property.似乎是一个类属性。 You cannot get the element with
getElementById
if the "btn-search" isn't the id of the element.如果“btn-search”不是元素的 id,则无法使用
getElementById
获取元素。
And you don't need to set "."而且您不需要设置“。” (class) or "#" (id) on the
getElementById
( getElementById
it's only to get elements by id, so you don't need to tell the script the property type you searching). (class) 或 "#" (id) 上的
getElementById
( getElementById
它只是通过 id 获取元素,所以你不需要告诉脚本你搜索的属性类型)。
As the user William Carneiro
stated, the issue is the .
正如用户
William Carneiro
所说,问题是.
character.特点。
The function getElementById
just receive the id, not a selector.函数
getElementById
只接收 id,而不是选择器。 Check documentation 检查文件
Change this line:改变这一行:
var input = document.getElementById('.search');
With something like this:像这样:
var input = document.getElementById('search');
... or this: ... 或这个:
var input = document.querySelector('#search');
Also, make sure that your element has id="search"
, it seems that probably you want to find an element with the class search instead.另外,请确保您的元素具有
id="search"
,看来您可能想要使用类搜索来查找元素。
var input = document.querySelector('.search');
I found this and I think it can helped you我找到了这个,我认为它可以帮助你
JavaScript: JavaScript:
function myFunction(event) {
var x = event.keyCode;
if (x == 27) {
// 27 is the ESC key
alert ("You pressed the Escape key!");
}
}
Ok most of your code was working好的,你的大部分代码都在工作
A few things to note需要注意的几点
'.search-btn'
'.search-btn'
的类名'search-btn'
'search-btn'
class='search-btn' id='search-btn' ...>
class='search-btn' id='search-btn' ...>
Other than that your code works除此之外,您的代码有效
function displayMatches() { const searchText = document.querySelector('.search').value; // the rest of your code here console.log("enter Worked Searching" ); } const suggestions = document.querySelector('.suggestions'); const searchBtn = document.querySelector('.btn-search'); searchBtn.addEventListener('click', displayMatches); var input = document.getElementById('search'); input.addEventListener("keyup", function(event) { if (event.keyCode === 13) { event.preventDefault(); document.getElementById('btn-search').click(); } });
<input class="search" id = 'search'type="text" placeholder="Search Deals"> <input type='button' class='btn-search' value='search' id ='btn-search'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.