簡體   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