簡體   English   中英

如何在Enter按下時觸發HTML密碼輸入

[英]How to trigger HTML password input on enter press

我想建立一個密碼提示屏幕,在輸入正確的密碼並按Enter后將您重定向到某個地方。 但是,當前只能通過單擊“提交”按鈕來工作。 感謝您對如何修改此代碼的任何幫助:

這是我的代碼現在的樣子:

 function checkPswd() { var confirmPassword = "asdf"; var password = document.getElementById("pswd").value; if (password == confirmPassword) { window.location.replace('http://www.google.com'); } } 
 @import url('https://fonts.googleapis.com/css?family=VT323'); body { background-color: #ffffff; display: table; } .div1 { height: 100%; width: 100%; display: table; position: absolute; top: 0; left: 0; } .div2 { text-align: center; display: table-cell; vertical-align: middle; font-family: VT323; font-size: 25px; color: #656262; } .box { text-align: center; font-size: 20px; font-family: VT323; outline: none; width: 250px; height: 35px; border: none; border-bottom: 2px solid #656262; } .confirm { border: none; font-size: 20px; font-family: VT323; margin-top: 10px; color: #656262; background-color: rgba(0, 0, 0, 0); cursor: pointer; } 
 <div class="div1"> <div class="div2"> <form> <input class="box" type="password" placeholder="123" id="pswd"> <br> <input class="confirm" type="button" value="SUBMIT" onclick="checkPswd();" /> </form> </div> </div> 

您可以使用keyup事件來檢測何時按下某個鍵,然后檢查其代碼是否為13 (即Enter的代碼)。 如果是這樣,您可以提交表格,或者致電checkPswd

碼:

/* Cache the password input. */
var pswd = document.getElementById("pswd");

/* Call 'checkPswd' when the 'Enter' key is released. */
pswd.onkeyup = function (e) {
   if (e.which == 13) checkPswd();
};

/* Prevent the form from submitting. */
pswd.parentElement.onsubmit = function () {
   return false;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM