jQuery 如何为“显示密码”功能创建 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14305727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:26:02  来源:igfitidea点击:

How to create HTML for a "show password" functionality?

jqueryhtmlbuttonpasswords

提问by sergserg

Possible Duplicate:
JQuery change type of input field

可能的重复:
JQuery 更改输入字段的类型

Demo here:

演示在这里:

http://jsbin.com/welcome/73795/edit

http://jsbin.com/welcome/73795/edit



<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


<input type="password" name="password" />
<button>Show Password</button>

$(document).ready(function() {
  $('button').click(function() {
    $('input').attr('type', 'text');
  });
});


I'm trying to create a button (or checkbox) that users will be able to toggle to see their password written in plain text.

我正在尝试创建一个按钮(或复选框),用户将能够切换以查看以纯文本形式写入的密码。

This is basically for portable devices where typing in passwords is a pain, and seeing it in plain text while writing is a UX improvement.

这基本上适用于输入密码很痛苦的便携式设备,而在编写时以纯文本形式查看它是一种用户体验改进。

In my example above, changing the type of the input doesn't work.

在我上面的示例中,更改输入的类型不起作用。

Any suggestions on how to tackle this problem?

关于如何解决这个问题的任何建议?

回答by Anujith

You can't change typeattribute.. See workaround here : http://jsfiddle.net/Ngtp7/2/

你不能改变type属性..见这里的解决方法:http: //jsfiddle.net/Ngtp7/2/

and a variation here : http://jsfiddle.net/Ngtp7/

和这里的一个变化:http: //jsfiddle.net/Ngtp7/