繁体   English   中英

根据用户输入重定向页面

[英]redirect page based on user input

我正在尝试做一件非常简单的事情,但我做不到。 我想根据用户在按下回车键时输入的输入值被重定向到特定页面。 但是,我不能每次都被重定向到同一页面。 我究竟做错了什么? 这是代码

    window.onkeyup = keyup;


var inputTextValue;

function keyup(e) {
  //setting your input text to the global Javascript Variable for every key press
  inputTextValue = e.target.value;
  console.log(inputTextValue);
 
  if (e.keyCode == 13) {
    if (inputTextValue == "myInput") {
      return true;
      //   window.location = "http://www.google.it";
    } else {
      return false;
      //   window.location = "http://www.facebook.it";
    }
  }
}

我使用window.location.href属性来更改页面的 url

 document.querySelector("#btn").addEventListener("click", () => { const text = document.querySelector("#inputRedirectText").value; if (text === "myName") { window.location.href = "https://www.google.com"; } else { window.location.href = "https://www.facebook.com"; } });
 <input type="text" id="inputRedirectText" placeholder="type..."/> <button id="btn">Done</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM