当用户单击“显示链接”时,显示密码,再次单击时隐藏密码
我正在尝试让这个简单的脚本发挥作用。基本上,当用户单击“显示”链接时,它将在密码文本框中显示密码,并在再次单击时隐藏它。我已经寻找解决方案,但找不到任何我需要的东西。这是代码:
JavaScript
function toggle_password(target){
var tag = getElementById(target);
var tag2 = getElementById("showhide");
if (tag2.innerHTML == 'Show'){
tag.setAttribute('type', 'text');
tag2.innerHTML = 'Hide';
}
else{
tag.setAttribute('type', 'password');
tag2.innerHTML = 'Show';
}
}
HTML
<label for="pwd0">Password:</label>
<input type="password" value="####" name="password" id="pwd0" />
<a href="#" onclick="toggle_password('pwd0');" id="showhide">Show</a>
当我单击链接时,没有任何反应。我也没有使用 if 语句对此进行了测试,但仍然没有执行任何操作。
I am trying to get this simple script to work. Basically, when a user clicks on the Show link, it will display the password in the password text box and hide it when it is clicked again. I have searched for solutions but couldn't find anything for what I need. Here is the code:
JavaScript
function toggle_password(target){
var tag = getElementById(target);
var tag2 = getElementById("showhide");
if (tag2.innerHTML == 'Show'){
tag.setAttribute('type', 'text');
tag2.innerHTML = 'Hide';
}
else{
tag.setAttribute('type', 'password');
tag2.innerHTML = 'Show';
}
}
HTML
<label for="pwd0">Password:</label>
<input type="password" value="####" name="password" id="pwd0" />
<a href="#" onclick="toggle_password('pwd0');" id="showhide">Show</a>
When I click the link, nothing happens. I have tested this without using the if statement too and still did nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您没有在
getElementById
上使用document
,您的
id
名称非法且难以使用:pwd'.$x。 '
你不能拥有其中一些字符。另外,此方法不适用于所有浏览器,在 IE < 9 例如,您只能在将元素附加到文档之前更改
.type
尝试交换它们:
希望这有帮助 -ck
you weren't using
document
on forgetElementById
your
id
names are illegal and difficult to work with:pwd'.$x.'
you can't have some of those chars.also, this method will not work in all browsers, in IE < 9 for instance you can only change
.type
before the element is attached to the documenttry swapping them:
hope this helps -ck
以下是使用
jQuery
(pastebin) 的示例:来源:
http://www.voidtricks。 com/password-show-hide-checkbox-click/
如何跨浏览器一致地对齐复选框及其标签
Here is an example using
jQuery
(pastebin):Sources:
http://www.voidtricks.com/password-show-hide-checkbox-click/
How to align checkboxes and their labels consistently cross-browsers
出于安全原因,您无法更改输入元素的类型。您必须用新元素替换整个元素。
Because of security reasons you can't change the type of an input element. You have to replace the entire element with a new one.
它真的很容易实现......我找到了这篇博客文章 它描述了用几行代码完成此操作的方法。我认为保留这样的选项不是一个好习惯,因为必须认真对待密码。
这是该页面的代码;它使用与您描述的相同的方法。但使用了一个复选框。实际上我已经在 oss' like linux 中看到了他指出的内容。他们甚至没有留下带有星号的密码痕迹。(但是 gui 的东西确实如此,不知道他们真的关心密码隐私,或者他们发现很难在命令行中做到这一点;-))
Its really easy to achieve...I found this blog post which describes the way of doing it with a few lines of code. I think its not a good practice to keep an option like this because passwords must be treated seriously.
here is the code from that page; it uses the same approach as you described. but uses a check-box.and practically i have seen what he points out in oss' like linux. they don't even leave a trace of the password with an asterisk.(but the gui thing does, don't know they really care about pw privacy or they find it difficult to do it in command line ;-) )
http://www.voidtricks.com/password-show-hide-checkbox -单击/
这是一个简单的教程,使用 jQuery 在选中复选框时在输入框中显示密码,并在取消选中复选框时隐藏密码。
Javascript
HTML
**
我们有一个带有“password”类的密码输入字段和一个带有 id“showHide”的复选框
使用复选框显示/隐藏密码的简单解决方案,这是菜鸟级别的复制/粘贴操作。完毕 !!! ;-)
http://www.voidtricks.com/password-show-hide-checkbox-click/
This is a simple tutorial to show a password in a input box when a checkbox is checked and hide it when the checkbox is unchecked using jQuery.
Javascript
HTML
*
*
We have a password input field with the class “password” and a checkbox with the id “showHide”
Easy solution for show / hide password using checkboxes, and this is noob level just copy/paste. DONE !!! ;-)
您可以使用内联脚本来简单地实现显示/隐藏密码。
You could use inline script for a simple implementation of show/hide password.
如果我们将文本框类型设置为
"passowrd"
,则不会显示字符,但是当我们将文本框类型设置为"text"
时,会在键入时显示字符。因此逻辑非常简单。我们可以通过JavaScript的
"setAttribute(type,newvalue)"
函数来改变任何HTML元素的类型。完整的代码示例可以在下面的链接中看到:
http://www.tutor4web.com/javascript/how-to-show-password-as-text-with-checkbox-using-javascript/
If we make a textbox type as
"passowrd"
then chars are not shown and but when we make a text-box type"text"
chars are shown as we type.Hence the logic is very simple. We can change the type of any HTML element by
"setAttribute(type,newvalue)"
function of JavaScript.A complete code example can be seen on below link:
http://www.tutor4web.com/javascript/how-to-show-password-as-text-with-checkbox-using-javascript/
你的解决方案可以是
your solution can be
虽然问题还有答案,但我还是把这个答案贴出来。
Although the question still has the answer, I am still posting this answer.
我相信这就是你想要的:
I believe this is how you wanted:
不要更改输入类型,更改文本大小。为隐藏设置非常小。
Don't change input type, change text size. Make it very small for Hide.