javascript - 如何使回车键单击搜索按钮
问题描述
如何让回车键按下搜索按钮?提前致谢。
function search(query) {
if (query.slice(0, 7) == "http://") {
// window.location.href = query
window.open(query, '_blank');
} else {
// window.location.href = "https://www.google.com/search?q=" + query
debugger;
window.open("https://www.google.com/search?q=" + query, '_blank');
}
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="box">
<div class="container-1">
<input type="search" id="search" placeholder="Search..." />
<div class="icon" onclick="search(document.getElementById('search').value)"><i class="fa fa-search"></i></div>
</div>
</div>
解决方案
这是一个例子:
// Get the input field
var input = document.getElementById("myInput");
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
document.getElementById("myBtn").click();
}
});
那应该回答你的问题。
推荐阅读
- javascript - “Access-Control-Allow-Origin”标头错误。响应具有 HTTP 状态代码 503
- amazon-web-services - 列出超过 x 天/月的 ami
- html - Flexbox - same height on all cards
- java - 试图理解java中的类继承
- reporting-services - 如何从多选 ssrs 报告参数中的多维数组中获取两个值?
- arrays - 使用 Powershell 的 ConvertFrom-Json 解析 JSON
- reactjs - 使用服务器端 React 和 React Router v4 在 Redux 操作中进行 URL 重定向
- wix - Wix - ServiceControl 启动需要四分钟才能失败,应该是 30 秒
- r - 如何使用 R 中的 dplyr 获取变量的总和,仅针对另一个变量的一个值
- c - linux lockf、flock和fcntl下C语言锁文件是什么意思?