JavaScript 错误"Uncaught TypeError: Cannot set property 'innerHTML' of null"



第51行出现错误

<!DOCTYPE html>
<html>
<head>
	<title>Validation</title>
</head>
<body>
	
<div style="width:500px;height:500px;padding-right:auto;padding-left:auto;">
<form action="#" method="get" onsubmit="return FormValidation();">
	
	
	<label>Current Password</label>
	<input type="text" name="pass" id="pass" maxlength="16" required><br>
	
	
	<label name="notif"></label>
	<input type="submit" name="submit">
	
</form>
<script type="text/javascript">
function FormValidation(){
		var passw = "against";
		var opass = document.getElementById("pass").value;
	
		
		if(opass != passw) {
			document.getElementById("notif").innerHTML = "Your password doesn't match.";
			alert('pass now match');
			return false;
		}
		else{
			document.getElementById("notif").innerHTML = opass + " " + npass + " " + rpass;
		}
	}
</script>
</div>
</body>
</html>

为什么会出现错误?我只是想发生的是定义密码,如果正确,并在标签上通知它。

document.getElementById("notif").innerHTML = "Your password doesn't match.";

这个表单为我的网站忘记密码。我们和我的团队正在开发一个带有用户帐户的网站,它是为一个贷款合作社。

这没有找到任何东西:

document.getElementById("notif")

因为没有元素具有那个id。因此,这将不返回任何内容,并且您无法访问未定义对象的属性。

可能最简单的直接方法是为目标元素提供您正在寻找的id值。我想你指的是这个元素:

<label name="notif" id="notif"></label>

事实上,label 可能根本不需要name属性。但我想这取决于你。

Edit:或者,如果您想避免修改标记,您可以使用document.querySelector代替:

document.querySelector('input[name="notif"]').innerHTML

您需要更改这一行:

<label name="notif"></label>

:

<label id="notif"></label>

如果必须保留name属性,可以尝试使用jQuery:

$("label[name='notif']").html('Your message');

相关内容

最新更新