3 回答
TA贡献1843条经验 获得超7个赞
你只是有一个“时间”问题:你应该在点击按钮的那一刻读取用户输入的值,你需要将你的var password = document.getElementById("pass").value行移到函数调用中。
const answer = "the password";
function pass() {
const password = document.getElementById("pass").value;
if (password == answer) {
document.getElementById("note").innerHTML = "<p>Good password!</p>";
}
else {
document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";
}
}
<input type="password" id="pass">
<button onclick="pass()">Go</button>
<div id="note"></div>
TA贡献1784条经验 获得超7个赞
您应该在函数内获取输入的值,如下所示:
var answer = "the password"
function pass() {
var password = document.getElementById("pass").value;
if (password == answer)
window.location.replace("http://example.com");
else
document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";
}
<input type="password" id="pass" />
<button onclick="pass()">Go</button>
<div id="note"></div>
TA贡献1836条经验 获得超3个赞
你好,有同样的错误,试试这个
var answer = "the password";
function pass() {
var password = document.getElementById("pass").value;
if (password === answer) {
window.location.replace("http://example.com");
}
else {
document.getElementById("note").innerHTML = "<p style='color:red'>Wrong username or password. Try again?</p>";
}
}
添加回答
举报