【问题标题】:How to set password and key event condition in js?如何在js中设置密码和关键事件条件?
【发布时间】:2019-09-20 09:22:02
【问题描述】:

如何在javascript中设置双重条件?

我的代码不起作用。使用正确的密码,window.location 不起作用

var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate() {

  var password = document.getElementById("password").value;

  if (password == "ad" && event.keyCode === 13) {
    window.location = "https://pac.com/arias"; // Redirecting to other page.
    return false;
  } else {
    attempt--; // Decrementing by one.
    alert("Contraseña incorrecta. Inténtalo de nuevo!");
    // Disabling fields after 3 attempts.
    if (attempt == 0) {

      document.getElementById("password").disabled = true;
      document.getElementById("submit").disabled = true;
      return false;
    }
  }
}

只有一个条件有效,但不是两个条件都有效...¿我做错了什么?

【问题讨论】:

  • ... && event.keyCode === 13?什么活动?此外,从代码中的 URL 来看,这看起来可能是为了在公共站点上运行。请注意,就安全性而言,这几乎是最弱的。
  • 另外请用HTML更新sn-p,保存前记得点击整理
  • 它仅用于演示工作。我想php来处理真正的安全性。谢谢
  • 另外如果你使用了提交事件,你不需要测试进入

标签: javascript logical-operators


【解决方案1】:

除了安全性差,你还想研究这个

var attempt = 3; // Variable to count number of attempts.
window.addEventListener("load", function() {
// Below function Executes on submit
  document.getElementById("login").addEventListener("submit", function(e) {
    if (document.getElementById("password").value.trim() !== "ad") {
      e.preventDefault();
      attempt--; // Decrementing by one.
      alert("Contraseña incorrecta."+(attempt>0?" Inténtalo de nuevo!":""));
      // Disabling fields after 3 attempts.
      document.getElementById("password").disabled = attempt <= 0
      document.getElementById("submitButton").disabled = attempt <= 0
    }
  })
})
<form id="login" action="https://pac.com/arias">
  pw: <input type="password" id="password" />
  <input type="submit" id="submitButton" />
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2021-12-31
    相关资源
    最近更新 更多