你肯定有没有在你的HTML代码中的其他元素具有相同的ID(密码或confirm_password)?
当我有这样的HTML
function checkPwdMatch() {
if (document.getElementById('password').value !=document.getElementById('confirm_password').value) {
document.getElementById('confirm_password').setCustomValidity('Two Passwords must match.');
} else {
document.getElementById('confirm_password').setCustomValidity('');
}
}
如果我把AAA和BBB的两个输入,这表明它们是无效的,当我把AAA和AAA一切似乎是确定。如果问题出在您发送表单和页面刷新后,您需要发送更多详细信息。