“将密码显示为文本"控制

2022-01-22 00:00:00 browser passwords jquery javascript

我有一个常用的登录表单,其中包含两个输入字段,一个用于登录,一个用于密码.我目前正在尝试添加一个控件,它将输入的密码显示为纯文本,以便用户可以检查它是否有错别字.

I have a usual login form consisting of two input fields, one for login, one for password. I am currently trying to add a control that will show entered password as plain text, so user can check it for typos.

问题是浏览器(至少是 Firefox)不允许动态更改输入字段的 type 属性,所以我不能只将 type="password" 更改为type="text".另一个问题是浏览器不允许获取密码字段的值,所以我不能创建一个新的 input type="text" 并将其值设置为密码的值.我见过几种不同的方法来完成这项任务,包括 这个,但它们只有在输入密码时才能工作,并且在浏览器自动填充密码时失败.

The problem is that browsers (at least Firefox) do not allow dynamic changing of type attribute of input fields, so I cannot just change type="password" to type="text". Another problem is that browsers do not allow to get value of password field, so I can't create a new input type="text" and set its value to the password's one. I've seen several different approaches to this task, including this one, but they are working only if the password is typed and fail when browser autofills the password.

因此,欢迎提出任何建议.我正在使用 jQuery.

So, any suggestions to do this are welcome. I am using jQuery.

推荐答案

你可以这样做:

<input type="password" id="password">
<input type="checkbox" onchange="document.getElementById('password').type = this.checked ? 'text' : 'password'"> Show password

相关文章