所以我正在做一个小项目,似乎无法使这个功能起作用。知道我做错了什么吗?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"></script>
<title>Login</title>
</head>
<body>
<form name="loginForm">
<label>Password:</label>
<input type="password" id="passwordField">
<input type="button" value="Login" onclick="checkPassword()">
<p><output id="outputField"></output></p>
</form>
</body>
</html>
这是脚本。
var passwordField = "apple"
var checkPassword = function (checkPassword, passwordField) {
if (passwordField == "apple") {
{document.loginForm.outputField.value = "You did it!";}
}
else {
{document.loginForm.outputField.value = "Fail.";}
}
};
答案 0 :(得分:1)
我的推荐
var passwordField = 'apple';
function checkPassword(input) {
return (input == 'apple') ? 'You did it!' : 'Fail';
};
这样称呼
<script>
document.write(checkPassword(passwordField));
</script>
答案 1 :(得分:0)
首先,在JavaScript中:
从方法checkPassword
中删除checkpassword
参数。你为什么需要它?
你不需要内括号,这没有任何意义。
var passwordField = "apple";
var checkPassword = function (passwordField) {
if (passwordField == "apple") {
document.loginForm.outputField.value = "You did it!";
}
else {
document.loginForm.outputField.value = "Fail.";
}
};
在HTML中:
您必须将密码传递给您的函数,否则它将为undefined
并且永远不会通过。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"></script>
<title>Login</title>
</head>
<body>
<form name="loginForm">
<label>Password:</label>
<input type="password" id="passwordField">
<input type="button" value="Login" onclick="checkPassword(document.getElementById('passwordField').value)">
<p><output id="outputField"></output></p>
</form>
</body>
</html>