我有一个表单提交,如果发生错误,我希望文本元素的边框是红色的。
if(isset($_POST["forgotsubmit"])){
if(!empty($_POST["forgotemail"])){
else{
echo "<script>document.getElementById('forgotemaill').style.border= 'border: 2px solid red';</script>";
}
}
}现在,在控制台中显示以下内容
forgot-password:61 Uncaught TypeError: Cannot read property 'style' of null
at forgot-password:61
(anonymous) @ forgot-password:61发布于 2018-01-29 21:11:30
当前错误指示它找不到元素。如果仔细观察,您会注意到JS中有一个拼写错误
document.getElementById('forgotemaill') <--这应该是forgotemail吗?
发布于 2018-01-29 21:12:34
看看你的javascript语法。试着去掉"border:“
echo "<script>document.getElementById('forgotemaill').style.border= '2px solid red';</script>";,你可以试试这个
echo "<script>document.getElementById('forgotemaill').setAttribute('style', 'border: 2px solid red');</script>";这应该是可行的,因为它将强制样式存在于对象上。
你的原创是假设'style‘存在,并且你能够改变它的值。
https://stackoverflow.com/questions/48502029
复制相似问题