Javascript 在给定特定输入时,如何重定向到特定页面

Javascript 在给定特定输入时,如何重定向到特定页面,javascript,html,input,url-redirection,Javascript,Html,Input,Url Redirection,这是我编写代码的第二天,所以如果我不知道如何正确解释或者这是一个基本的解决方案,请原谅我 我试图在一个网站上复制一个控制台界面,我希望输入框仅在给定某个输入时重定向到某个链接。我也希望它不重定向,并给出一个消息,如果它给出了一个无效的提示。 到目前为止,输入框的代码是 <form action="backlog"> <label for="fname"> <input type="text" name="logsarchives" id

这是我编写代码的第二天,所以如果我不知道如何正确解释或者这是一个基本的解决方案,请原谅我

我试图在一个网站上复制一个控制台界面,我希望输入框仅在给定某个输入时重定向到某个链接。我也希望它不重定向,并给出一个消息,如果它给出了一个无效的提示。 到目前为止,输入框的代码是

  <form action="backlog">
    <label for="fname">     
    <input type="text" name="logsarchives" id="logsarchives" autocomplete="off"  placeholder="Enter Console Command">
    </form>

到目前为止,可以查看代码,也可以查看网站。
谢谢你阅读这篇文章

您可以使用javascript函数提交表单,获取表单中输入的值,然后检查是否匹配

Html:

您可能需要删除表单的action属性,这会使提交自动重定向。 此外,只有在提交表单时(通过按下按钮或键盘上的enter键)才有效。如果希望表单自动工作,则可以搜索输入元素的onChange方法


这是一支你可以玩的钢笔!:)

您可以使用javascript函数提交表单,获取表单中输入的值,然后检查是否匹配

Html:

您可能需要删除表单的action属性,这会使提交自动重定向。 此外,只有在提交表单时(通过按下按钮或键盘上的enter键)才有效。如果希望表单自动工作,则可以搜索输入元素的onChange方法

这是一支你可以玩的钢笔!:)

添加javascript:

function myFormSubmit(event) {
  event.preventDefault();

  const myInput = document.getElementById('logsarchives');

  if (myInput.value == 'something') {
    window.location = 'http://google.com'
  }
}
<script>
document.getElementById("logsarchives").addEventListener("keydown", function() {
    var valid-options = ["option1", "option2", "soforth"];
    var input = document.getElementById("logsarchives").value;
    for (var i = 0; i < valid-options.length, i++) {
        if(input === valid-options[i]) { /*or ==*/
            window.location.replace("/backlog?logsarcives=" + valid-options[i]);
            return;
        }
    }
});
</script>

document.getElementById(“logsarchives”).addEventListener(“keydown”,function()){
var有效选项=[“选项1”、“选项2”、“soforth”];
var input=document.getElementById(“logsarchives”).value;
对于(var i=0;i
添加javascript:

function myFormSubmit(event) {
  event.preventDefault();

  const myInput = document.getElementById('logsarchives');

  if (myInput.value == 'something') {
    window.location = 'http://google.com'
  }
}
<script>
document.getElementById("logsarchives").addEventListener("keydown", function() {
    var valid-options = ["option1", "option2", "soforth"];
    var input = document.getElementById("logsarchives").value;
    for (var i = 0; i < valid-options.length, i++) {
        if(input === valid-options[i]) { /*or ==*/
            window.location.replace("/backlog?logsarcives=" + valid-options[i]);
            return;
        }
    }
});
</script>

document.getElementById(“logsarchives”).addEventListener(“keydown”,function()){
var有效选项=[“选项1”、“选项2”、“soforth”];
var input=document.getElementById(“logsarchives”).value;
对于(var i=0;i
所以您需要一点JavaScript来处理这种情况。此外,没有适当的结束目的,甚至没有结束标记,只使用了不必要的标记数量

<!DOCTYPE html>
<html> 
    <body>
        <input type="text" name="logsarchives" id="logsarchives" autocomplete="off"  placeholder="Enter Console Command" onkeydown="redirect(this)">
    </body>
    <script>
        function redirect(ele) {
            if(event.key === 'Enter') {
                if(ele.value === 'google')
                     window.location.href = 'https://google.com';
                else
                    alert("Invalid");
            }
        }
    </script>
</html>

函数重定向(ele){
如果(event.key=='Enter'){
如果(ele.value==='google')
window.location.href=https://google.com';
其他的
警报(“无效”);
}
}

所以您需要一点JavaScript来处理这种情况。此外,没有适当的结束目的,甚至没有结束标记,只使用了不必要的标记数量

<!DOCTYPE html>
<html> 
    <body>
        <input type="text" name="logsarchives" id="logsarchives" autocomplete="off"  placeholder="Enter Console Command" onkeydown="redirect(this)">
    </body>
    <script>
        function redirect(ele) {
            if(event.key === 'Enter') {
                if(ele.value === 'google')
                     window.location.href = 'https://google.com';
                else
                    alert("Invalid");
            }
        }
    </script>
</html>

函数重定向(ele){
如果(event.key=='Enter'){
如果(ele.value==='google')
window.location.href=https://google.com';
其他的
警报(“无效”);
}
}

佩德罗·斯图默的答案是否让你满意?佩德罗·斯图默的答案是否让你满意?