我正在尝试做一件非常简单的事情,但是我做不到。我想根据用户在按Enter键时输入的输入值重定向到特定页面。但是我不能,并且每次将我重定向到同一页面时。我究竟做错了什么 ?这是代码
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";
}
}
}
答案 0 :(得分:0)
我使用了 window.location.href
属性来更改页面的网址
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>