[英]How to get enter key to emulate a button press?
I'm developing a weather app, and I only want the code to execute once the Enter key is pressed or when a user clicks on Submit.我正在开发一个天气应用程序,我只希望在按下 Enter 键或用户单击提交时执行代码。 The problem is, the code executes whenever any key is pressed, and I'm not sure why?
问题是,只要按下任何键,代码就会执行,我不知道为什么? It wouldn't ordinarily be a big deal, but it's requesting the API every time and I only get 60 requests a minute, so two or three searches in that time will reach that limit.
这通常没什么大不了的,但它每次都请求 API 而我每分钟只收到 60 个请求,所以在那段时间里有两三个搜索会达到这个限制。
let button = document.querySelector("#button");
let searchBox = document.querySelector("#search-box");
let city = document.querySelector(".city");
let feelsLike = document.querySelector(".feels-like");
let temperature = document.querySelector(".temp");
let weatherDescription = document.querySelector(".weather");
let windSpeed = document.querySelector(".wind");
let icons = document.querySelector(".icons");
searchBox.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
document.getElementById("button").click();
}
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
searchBox.value +
"&units=metric&appid="
)
...Rest of code to be executed
)};
I think it would make sense for the enter key to emulate a button press, but I'm not entirely sure how to do that - and any resources I've used online haven't helped, unfortunately.我认为输入键模拟按钮按下是有意义的,但我不完全确定如何做到这一点 - 不幸的是,我在网上使用的任何资源都没有帮助。
In your listener searchBox.addEventListener("keypress", function (event) {..}
you're executing the fetch
function everytime when a keypress occurs. Since the if
-condition does not enclosure the your fetch
execution.在您的侦听
searchBox.addEventListener("keypress", function (event) {..}
中,您正在执行fetch
function 每次按键发生时。因为if
-condition 不会包含您的fetch
执行。
Try this:尝试这个:
if (event.key === "Enter") {
document.getElementById("button").click();
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
searchBox.value +
"&units=metric&appid="
)
}
You should put the (fetch) logic in the click handler for the button.您应该将(获取)逻辑放在按钮的单击处理程序中。
Some other points:其他几点:
fetch
, then put that code in a chained then
callback.fetch
获得响应后需要执行代码,则将该代码放入链式then
回调中。 Note that the promise that fetch
returns, resolves to a response object, and you'll need to call one of its methods to get yet another promise back, which in turn will resolve to the actual data.fetch
返回的 promise 会解析为响应 object,并且您需要调用其中一个方法来获取另一个 promise,这反过来将解析为实际数据。.click()
..click()
。 It is better to put the targeted code in a named function, and then call that function both here, and in the button's click handler.getElemebtById
.getElemebtById
。function process() {
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
searchBox.value +
"&units=metric&appid="
).then(function (response) {
return response.json(); // <-- maybe? or .text() ?
}).then(function (data) {
//...Rest of code to be executed
});
});
button.addEventListener("click", process);
searchBox.addEventListener("keypress", function (event) {
if (event.key === "Enter") process();
)};
As stated by Amacado, the fetch is outside the if.正如 Amacado 所说,获取在 if 之外。
Moreover, if the search box is actually an HTML <input type="search">
element, there is already an OnSearch event handler that takes account of both the enter key and the magnifying glass icon in the phone keyboard ( search event ).此外,如果搜索框实际上是一个 HTML
<input type="search">
元素,那么已经有一个 OnSearch 事件处理程序,它考虑了回车键和手机键盘中的放大镜图标(搜索事件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.