极其简单的密码系统无法正常工作
Incredibly Simple Password System Not Working
所以我得到了这个简单的代码:
<!DOCTYPE html>
<html>
<head>
<title>SuM BUTtonsS DOe</title>
<link rel="stylesheet" href="buttons.css"/>
</head>
<body>
<p>Please enter the password</p>
<form id="enter" onSubmit="javascript:passCheck()">
<input id="password" type="password" placeholder="Password">
</form>
<script type="text/javascript">
var input = document.getElementById('password');
function passCheck() {
if (input == 'herro') {
window.alert("IT WORKS!!");
}
else {
window.alert("darn");
}
}
</script>
</body>
</html>
应该不会太难吧?问题是它不起作用...
我检查过的所有地方似乎都说您唯一需要做的就是 getElementById,这将为您提供文本字段的内容。但是,我一直收到 "darn" 警报。有任何想法吗?我知道正在调用函数...也许输入必须在函数中?
您正在根据字符串检查 DOM 元素。 getElementById 方法 returns 一个 DOM 元素。您要做的是根据字符串检查元素值。
将条件更改为
input.value == 'herro'
应该可以解决问题。
尝试获取字段的值,而不是字段本身:
var input = document.getElementById('password').value;
所以我得到了这个简单的代码:
<!DOCTYPE html>
<html>
<head>
<title>SuM BUTtonsS DOe</title>
<link rel="stylesheet" href="buttons.css"/>
</head>
<body>
<p>Please enter the password</p>
<form id="enter" onSubmit="javascript:passCheck()">
<input id="password" type="password" placeholder="Password">
</form>
<script type="text/javascript">
var input = document.getElementById('password');
function passCheck() {
if (input == 'herro') {
window.alert("IT WORKS!!");
}
else {
window.alert("darn");
}
}
</script>
</body>
</html>
应该不会太难吧?问题是它不起作用...
我检查过的所有地方似乎都说您唯一需要做的就是 getElementById,这将为您提供文本字段的内容。但是,我一直收到 "darn" 警报。有任何想法吗?我知道正在调用函数...也许输入必须在函数中?
您正在根据字符串检查 DOM 元素。 getElementById 方法 returns 一个 DOM 元素。您要做的是根据字符串检查元素值。
将条件更改为
input.value == 'herro'
应该可以解决问题。
尝试获取字段的值,而不是字段本身:
var input = document.getElementById('password').value;