这是我的代码:
<html>
<body>
<script type="text/javascript">
function CheckPwd()
{
if(document.forms['frm'].pass.value == "magicisreal")
{
alert('Welcome, ADMIN...');
return true;
}
return false;
}
</script>
Password:
<br>
<input type="password" name="pass">
<br><br>
<input type="submit" value="Continue" onclick="return CheckPwd()">
</body>
</html>
当在文本框中输入值“magicisreal”时,它应该会返回一个警报。到底是我做错了什么没有导致这个结果?
最佳答案
首先,没有名为frm 的表单。 因此,您正在检查的 if 语句无效。 使用以下代码片段来实现您正在寻找的内容:
<html>
<body>
<script type="text/javascript">
function CheckPwd()
{
var password = document.getElementById('pass-input').value;
console.log(password);
if(password == "magicisreal")
{
alert('Welcome, ADMIN...');
return true;
}
return false;
}
</script>
Password:
<br>
<input type="password" id="pass-input" name="pass">
<br><br>
<input type="submit" value="Continue" onclick="CheckPwd()">
</body>
</html>
关于javascript - 为什么不检查正确的值? (Javascript/HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55786500/