Javascript 与setCustomValidity有关的问题
我正在编写一个简单的if语句来测试每个输入框中的密码是否匹配。如果它们都匹配,则不会给出错误,如果它们不匹配,则使用.setCustomValidity()给出错误“它们不匹配”。我遇到的问题是,当给出错误,然后将密码更正为匹配时,仍然会给出错误。我不确定我做错了什么。下面是我的代码和一个到工作JSfiddle的链接 JSfiddle: HTML:Javascript 与setCustomValidity有关的问题,javascript,html,Javascript,Html,我正在编写一个简单的if语句来测试每个输入框中的密码是否匹配。如果它们都匹配,则不会给出错误,如果它们不匹配,则使用.setCustomValidity()给出错误“它们不匹配”。我遇到的问题是,当给出错误,然后将密码更正为匹配时,仍然会给出错误。我不确定我做错了什么。下面是我的代码和一个到工作JSfiddle的链接 JSfiddle: HTML: 您正在执行的错误是customValidity仍然是“它们不匹配”,因为您没有将其设置为空字符串(浏览器将其视为成功验证),因此输入在一次验证失败后
您正在执行的错误是customValidity仍然是
“它们不匹配”
,因为您没有将其设置为空字符串(浏览器将其视为成功验证),因此输入在一次验证失败后仍保持相同的状态
submit.onclick = function() {
// there is no need to redefine these two variables
var firstPassword = firstPasswordInput;
var secondPassword = secondPasswordInput;
if(firstPassword.value !== secondPassword.value) {
firstPassword.setCustomValidity("they do not match");
}
//add this part
else {
firstPassword.setCustomValidity("");
}
}
您正在执行的错误是customValidity仍然是
“它们不匹配”
,因为您没有将其设置为空字符串(浏览器将其视为成功验证),因此输入在一次验证失败后仍保持相同的状态
submit.onclick = function() {
// there is no need to redefine these two variables
var firstPassword = firstPasswordInput;
var secondPassword = secondPasswordInput;
if(firstPassword.value !== secondPassword.value) {
firstPassword.setCustomValidity("they do not match");
}
//add this part
else {
firstPassword.setCustomValidity("");
}
}
@ImranAli运算符中没有输入错误,
==
是一个有效的js相等运算符。@ImranAli运算符中没有输入错误,==
是有效的js相等运算符。
submit.onclick = function() {
// there is no need to redefine these two variables
var firstPassword = firstPasswordInput;
var secondPassword = secondPasswordInput;
if(firstPassword.value !== secondPassword.value) {
firstPassword.setCustomValidity("they do not match");
}
//add this part
else {
firstPassword.setCustomValidity("");
}
}