我有这两个HTML元素,它们是隐藏的,当有人输入错误的凭据时,它们应该是可见的。
用户名
<div class="alert alert-danger login_alert" id="username_alert" role="alert"><span>Your username is incorrect. Please try again.</span></div>
密码
<div class="alert alert-danger login_alert" id="password_alert" role="alert"><span>Your password is incorrect. Please try again.</span></div>
在php中,当密码错误时,我会尝试下面这行代码:
echo '<script>console.log("Error: Password incorrect"); document.getElementById(password_alert).style.display = "block";</script>';
这个console.log会显示在控制台中,但之后我会看到:
Uncaught ReferenceError: password_alert is not defined
at login.php:15
这是在我的div
中定义的password_alert
。我也尝试使用ClassName()
为用户名和密码创建两个login_alert
类,但这也不起作用。
发布于 2018-10-25 04:07:51
实际上,您必须更改
document.getElementById(password_alert)
为
document.getElementById("password_alert")
从getElementById Manual的定义
发布于 2018-10-25 15:52:10
根据@RobinZigmond的建议,通过将整个PHP部分移到HTML <body>
的末尾,解决了这个问题。
https://stackoverflow.com/questions/52977112
复制相似问题