gpt4 book ai didi

javascript - 按键盘上的回车键进行搜索

转载 作者:行者123 更新时间:2023-12-01 00:18:32 24 4
gpt4 key购买 nike

尝试按 Enter 键进行搜索。可以与按钮一起使用,但由于某种原因,我的按键代码不起作用。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();
}
});

最佳答案

在:

document.getElementById('.btn-search').click();

您确定 .btn-search 是 id 属性吗?看起来像是类(Class)属性(property)。如果“btn-search”不是元素的 id,则无法使用 getElementById 获取元素。

而且你不需要设置“.” (class) 或 getElementById 上的“#”(id) (getElementById 它只是通过 id 获取元素,所以你不需要告诉脚本属性类型您正在搜索)。

关于javascript - 按键盘上的回车键进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com