javascript - 未达到事件侦听器内部的 JS for 循环
问题描述
我正在构建一个简单的 html 表单并在登录按钮上单击我更新 url,但问题是未达到按钮事件侦听器内部的 for 循环。
var user_credentials = {
"andrea@gmail.com": "password",
"hospitalagency@gmail.com": "hospitalagency",
"user@gmail.com": "password"
}
const loginForm = document.getElementById("form");
const loginButton = document.getElementById("login-form-submit");
loginButton.addEventListener("click", (e) => {
// e.preventDefault();
const email = loginForm.email.value;
const password = loginForm.password.value;
for (let i = 0; i < user_credentials.length; i++) {
if (Object.keys(user_credentials)[i] === email) {
if(Object.values(user_credentials)[i] === password) {
console.log("hi");
window.location.pathname = "/html/home.html";
}
} else {
window.location.reload();
}
}
});
嗨永远不会“打印”,我不知道它是否是正确的词,但永远不会达到 for 循环。我试图在网上搜索如何调试,但我无法做到。loginForm 和 loginButton 不是问题,因为我尝试使用 console.log() 并且它们被很好地检测到。如果您对代码有任何更正,欢迎他们
解决方案
user_credentials.length
将始终未定义,因为它的对象,请尝试将其更改为 Array。
推荐阅读
- typescript - 对对象文字强制执行 TypeScript 联合类型类型检查
- wordpress - 面包屑的 Wordpress 自定义层次结构(并且没有 slug / URL 的层次结构)
- javascript - React - 如何为地图功能的每个元素设置内联CSS?
- sql - 如何通过在 MSSQL 中使用 Pivot with Month 来获取每个特定月份的 Rating 列的值及其定义
- c# - ExcludeFromCodeCoverageAttribute 在 azuredevops 管道中不起作用
- javascript - 为什么我的 for 循环有时会执行两次,尽管我使用了 break 语句?
- ssas-tabular - AnalysisServices:以表格形式对表进行可视化分组
- reactjs - Highmaps 加勒比地图
- java - 客户端 (X509) 认证的默认环境应该是什么
- ios - 如何将多个视图转换到同一位置(CGPoint)