无法停止表单提交



我无法阻止提交此表单(如果验证失败(。表单具有onSubmit="return validateThisFrom (this);"属性。

<form action="php/create-user.php" method="POST" onSubmit="return validateThisFrom (this);">

如果密码不匹配,validate函数应返回false。这些是密码字段及其ID。

<input type="password" id="password">
<input type="password" id="confirm_password">

这是的功能

function validateThisFrom(thisForm) {
var pwd1 = document.getElementById('password').value;
var pwd2 = document.getElementById('confirm_password').value;

if (thisForm.password.value != thisForm.confirm_password.value) {
document.getElementById('message').style.color = '#dc3545';
document.getElementById('message').style.fontSize = '13px';
document.getElementById('message').innerHTML = '<b>The passwords don't match!</b>.';
document.getElementById('password').style.borderColor = '#dc3545';
document.getElementById('confirm_password').style.borderColor = '#dc3545';
document.getElementById('button-addon1').style.borderColor = '#dc3545';
document.getElementById('button-addon2').style.borderColor = '#dc3545';

return false;
}
}

出现消息The password don't Match,但随后表单正在提交,即使函数返回false。

我已经通过限制网络速度(我不确定这是否真的有效(成功地拍下了它的截图

正如你在照片中看到的,消息出现了,但随后表单正在提交。

我在自己的电脑上检查了你的代码,你的if中的一个元素似乎有问题。当我删除所有这些document.getElementById…并执行程序时,它会正常工作。检查所有这些元素,并确保它们都存在于html文件中,并且您已经正确地编写了它们的id。当我把它们添加到我的代码中时,问题解决了。

最新更新