[英]redirect page based on user input
I'm trying to do a very simple thing, but I can't.我正在尝试做一件非常简单的事情,但我做不到。 I would like to be redirected to a specific page depending on the input value entered by the user on pressing the enter key.
我想根据用户在按下回车键时输入的输入值被重定向到特定页面。 However I can't and every time I am redirected to the same page.
但是,我不能每次都被重定向到同一页面。 What am I doing wrong?
我究竟做错了什么? this is the code
这是代码
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";
}
}
}
I used window.location.href
property to change the url of page我使用
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.