javascript - 我正在编写函数,当在 textInput 中的值写为“giorgi”并且在 passInput 中写为“gio”时,警报欢迎,如果不是,则警报错误
问题描述
let userName = document.getElementById('textInput').value;
let userPass = document.getElementById('passInput').value;
function myFunction(){
if(userName === 'giorgi' && userPass === 'gio'){
alert('Welcome');
} else {
alert('Wrong');
}
}
解决方案
您只读取一次输入,此时它们是空的。将输入值分配给变量不会创建连接,它只是分配执行分配时输入中的任何字符串。如果稍后它们中的任何一个发生变化,那根本不会影响另一个。
您希望在用户单击登录按钮时读取输入的值。
function login() {
// notice we're reading and assigning the input values every time this function runs
let userName = document.getElementById('userName').value;
let userPass = document.getElementById('userPass').value;
if (userName === 'giorgi' && userPass === 'gio') {
alert('Welcome');
} else {
alert('Wrong');
}
}
<input type=text id=userName placeholder="Enter Username" />
<input type=password id=userPass placeholder="Enter Password" />
<button type=button onclick=login()>Login</button>
推荐阅读
- javascript - 导入的函数不在 Firefox 调试控制台的范围内
- css - 使用 CSS 重新排序 Squarespace 块
- tensorflow - 如何在 TensorFlow 中自动分配空闲 GPU
- r - 如何计算多列连续行的差异?
- ruby - 如何继续
- javascript - 未捕获的类型错误:icon.hasName 不是函数
- spacy - 如何在 spaCy 的 Doc.to_disk(path, exclude=['user_data']) 中指定要排除的键/值对?
- haskell - 检查树是否是另一棵树的子树
- python - 比较两个数组中的值与方差
- firebase - 无法将 firebase 连接到颤振应用程序(android 部分)