我正在尝试让网站显示不同的消息,具体取决于输入的密码是否正确(正确的密码是密码)。到目前为止,没有运气。
根据Chrome,错误在此行(函数的第一行):
if(document.getElementById(“psw”)=“thePassword”){
JSFiddle:https://jsfiddle.net/5grtmxd4/
提前致谢!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form>
Password<br>
<input id="psw" type="password" name="password" size="45" autofocus></input><br><br>
<input type="submit" value="Submit" onclick="message();"></input>
</form>
<script>
function message () {
if (document.getElementById("psw") = "thePassword") {
document.getElementById("display").innerHTML = "Logged in";
} else {
document.getElementById("display").innerHTML = "Try again!";
}}
</script>
<p id="display"></p>
</body>
</html>
编辑:我很感激,不是试图重写整个代码,而只是进行必要的更改。谢谢!
答案 0 :(得分:2)
更改
if(document.getElementById("psw") = "thePassword")
if(document.getElementById("psw").value == "thePassword")
答案 1 :(得分:1)
在==
语句中使用double equals(if
)而不是单一等号(=
)
if (document.getElementById("psw").value == "thePassword")
而不是
if (document.getElementById("psw") = "thePassword")
答案 2 :(得分:1)
您的代码有几个问题:
请在https://jsfiddle.net/5grtmxd4/1/
找到更新版本function message() {
if (document.getElementById("psw").value == "thePassword") {
document.getElementById("display").innerHTML = "Logged in";
} else {
document.getElementById("display").innerHTML = "Try again!";
}
}