Javascript 如何触发搜索按钮从用户输入转到网页?
我正在制作我的第一个搜索栏/引擎。我想让搜索栏响应用户输入,这样它就可以将用户输入发送到特定的网站。我的问题是,当用户从Javascript 如何触发搜索按钮从用户输入转到网页?,javascript,html,Javascript,Html,我正在制作我的第一个搜索栏/引擎。我想让搜索栏响应用户输入,这样它就可以将用户输入发送到特定的网站。我的问题是,当用户从中的文本输入某些关键字时,我的搜索按钮没有将用户发送到网页 //搜索引擎功能 var sForm=document.getElementById(“srchFrm”); document.addEventListener(“单击”),函数(事件){ var clickedInside=sForm.contains(event.target); 如果(单击左侧){ //显示搜索
中的文本输入某些关键字时,我的搜索按钮没有将用户发送到网页
//搜索引擎功能
var sForm=document.getElementById(“srchFrm”);
document.addEventListener(“单击”),函数(事件){
var clickedInside=sForm.contains(event.target);
如果(单击左侧){
//显示搜索建议
document.getElementById(“srchRslts”).style.display=“block”;
}否则{
//隐藏搜索建议
document.getElementById(“srchRslts”).style.display=“无”;
}
});
//学校学分
函数searchIngressults(){
//声明变量
让输入,过滤,ul,li,a,i,txtValue;
输入=document.getElementById('srchBar');
filter=input.value;
ul=document.getElementById(“srchRslts”);
li=ul.getElementsByTagName('li');
//循环浏览所有列表项,并隐藏与搜索查询不匹配的项
对于(i=0;i-1){
李[i].style.display=“”;
}否则{
li[i].style.display=“无”;
}
}
//归功于Textfixer.com和https://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box 搜索按钮代码。
//获取搜索按钮
var submitButton=document.getElementById(“sbmtBtn”);
//将事件侦听器添加到提交按钮
input.addEventListener(“键控”,函数(e){
e、 预防默认值();
//按enter键激活搜索引擎
如果(event.keyCode===13){
submitButton.click();
}
});
函数cSbmtBtn(){
a=li[i].getElementsByTagName(“a”)[0];
txtValue=a.textContent | | a.innerText;
如果(过滤器==txtValue){
submitButton.value=txtValue;
}
}
}
html
搜索
script.js
// Search engine functionality
var sForm = document.getElementById("srchFrm");
var input = document.getElementById('srchBar');
var anchors = document.querySelectorAll("form ul li a");
var anchorTexts = anchors.map (anchor => anchor.textContent);
var matchedAnchors = [];
// Document click listener to detect clicks inside sForm
document.addEventListener("click", function(event) {
// Document click handler logic
});
// Submit handler to prevent default form submission.
// Form submission is also triggered when the submit button is clicked
// So the logic of the submit button can be moved here
sForm.addEventListener("submit", function(event) {
event.preventDefault(); // Necessary to prevent form submission which will redirect the page to the URL in the `action` attribute of the form
// Form submission logic goes here
// There can be multiple anchors matching the text, so I'll assume you want the first match
if (matchedAnchors.length === 0) return;
// Change the window location to the href of the first matched anchor
window.location.href = matchedAnchors[0].getAttribute("href")
});
// Input key up handler. Logic for what happens on key up goes here
input.addEventListener("keyup", function(event) {
var inputValue = event.target.value; // or input.value
matchedAnchors.splice(0); // Clear the matchedAnchors array before starting a new search
// Make all anchors visible before starting a new search
for (const anchor of anchors) anchor.style.display = "inline-block"
// Find anchors that match the input
for (const i = 0; i < anchorTexts.length; i++) {
const anchorText = anchorTexts[i];
if (anchorText.startsWith(inputValue)) { // Or 'indexOf' if you want a partial match starting from anywhere in the string
matchedAnchors.push (anchors[i]);
}
}
// Find anchors that match the input
for (const anchor of anchors) {
if (matchedAnchors.includes(anchor)) {
anchor.style.display = "inline-block";
} else anchor.style.display = "none";
}
})
//搜索引擎功能
var sForm=document.getElementById(“srchFrm”);
var input=document.getElementById('srchBar');
var anchors=document.queryselectoral(“表格ul li a”);
var anchorTexts=anchors.map(anchor=>anchor.textContent);
var matchedAnchors=[];
//文档单击侦听器以检测sForm内的单击
document.addEventListener(“单击”),函数(事件){
//文档单击处理程序逻辑
});
//提交处理程序以防止默认表单提交。
//单击“提交”按钮时也会触发表单提交
//因此,提交按钮的逻辑可以移到这里
sForm.addEventListener(“提交”,函数(事件){
event.preventDefault();//防止表单提交所必需的,表单提交会将页面重定向到表单的'action'属性中的URL
//表单提交逻辑在这里
//可以有多个锚点匹配文本,因此我假设您想要第一个匹配
if(matchedAnchors.length==0)返回;
//将窗口位置更改为第一个匹配锚点的href
window.location.href=matchedAnchors[0].getAttribute(“href”)
});
//输入键向上处理程序。在钥匙上发生的事情的逻辑是这样的
input.addEventListener(“键控”,函数(事件){
var inputValue=event.target.value;//或input.value
matchedAnchors.splice(0);//在开始新搜索之前清除matchedAnchors数组
//在开始新搜索之前,使所有定位可见
对于(锚定的常量锚定)anchor.style.display=“内联块”
//查找与输入匹配的锚定
for(常数i=0;i
这种类型的东西应该可以帮助您解决…您得到了什么实际行为…?当我单击搜索按钮时,它会显示“未找到”。由于您在脚本中处理表单提交,您应该使用e.preventDefault()
向表单添加onsubmit
事件处理程序,以防止它重定向页面。。。这应该可以解决“找不到”的问题…我应该将onsubmit事件处理程序函数放在哪里才能使搜索按钮工作?我尝试将代码输入到我的JS文件中,这比我以前单击提交按钮时没有“找不到”的代码要好。我现在的问题是如何从
标签中获取文本,并将其发送到JS,以便在单击提交按钮时转到该特定链接?我已经用代码更新了答案,我认为这些代码应该有助于实现您的目标…感谢您的更新。这是一个进步
// Search engine functionality
var sForm = document.getElementById("srchFrm");
var input = document.getElementById('srchBar');
var anchors = document.querySelectorAll("form ul li a");
var anchorTexts = anchors.map (anchor => anchor.textContent);
var matchedAnchors = [];
// Document click listener to detect clicks inside sForm
document.addEventListener("click", function(event) {
// Document click handler logic
});
// Submit handler to prevent default form submission.
// Form submission is also triggered when the submit button is clicked
// So the logic of the submit button can be moved here
sForm.addEventListener("submit", function(event) {
event.preventDefault(); // Necessary to prevent form submission which will redirect the page to the URL in the `action` attribute of the form
// Form submission logic goes here
// There can be multiple anchors matching the text, so I'll assume you want the first match
if (matchedAnchors.length === 0) return;
// Change the window location to the href of the first matched anchor
window.location.href = matchedAnchors[0].getAttribute("href")
});
// Input key up handler. Logic for what happens on key up goes here
input.addEventListener("keyup", function(event) {
var inputValue = event.target.value; // or input.value
matchedAnchors.splice(0); // Clear the matchedAnchors array before starting a new search
// Make all anchors visible before starting a new search
for (const anchor of anchors) anchor.style.display = "inline-block"
// Find anchors that match the input
for (const i = 0; i < anchorTexts.length; i++) {
const anchorText = anchorTexts[i];
if (anchorText.startsWith(inputValue)) { // Or 'indexOf' if you want a partial match starting from anywhere in the string
matchedAnchors.push (anchors[i]);
}
}
// Find anchors that match the input
for (const anchor of anchors) {
if (matchedAnchors.includes(anchor)) {
anchor.style.display = "inline-block";
} else anchor.style.display = "none";
}
})