我正在尝试寻找解决方案,是否可以同时显示/隐藏密码?
我只能显示/隐藏第一个原始密码。 有谁知道解决方案或者是否可能?
这是 JavaScript 和 HTML 代码
function Toggle() {
let btt = password
if (btt.type === "password") {
btt.type = "text";
} else {
btt.type = "password";
}
}
function checkPassword(register) { //correct
let fisrtPassword = register.password.value;
let retypedPassword = register.retypedpassword.value;
if (fisrtPassword === retypedPassword) {
return true;
} else {
passwordMatchError.setAttribute("class", "error");
passwordMatchError.innerHTML = ("Passwords do not match. Please retype");
return false;
}
}
最佳答案
在您的 Toggle
函数中,您当前仅使用 id password
更新字段。因此,当您更改该字段的类型时,只会更改该字段。您没有检查另一个名为 retypedpassword
的字段,因此它不会被更改。请查看我在下面添加的代码,了解两个字段的切换的工作版本。
function Toggle() {
const fields = [password, retypedpassword]
fields.forEach( x => {
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
});
}
第二个示例使用当前代码的样式:
function Toggle() {
let btt = password
if (btt.type === "password") {
btt.type = "text";
} else {
btt.type = "password";
}
let otherfield = retypedpassword
if (otherfield.type === "password") {
otherfield.type = "text";
} else {
otherfield.type = "password";
}
}
关于javascript - 有什么方法可以在Javascript中同时显示隐藏多个密码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646264/