jQuery 将输入类型文本转换为密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18539052/
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 21:53:48  来源:igfitidea点击:

Transform input type text to password

jqueryattr

提问by user2501504

I want to change the input type to password when I click on it. I tried this:

当我点击它时,我想将输入类型更改为密码。我试过这个:

<input name="pin" id="cp_serv" type="text" class="pin_v" value="Insert PIN Please" onclick="this.value='';$(this).attr('type','password');"/>

I tried to use the jQuery function attrto change this on the clickevent, but nothing is happening.

我尝试使用 jQuery 函数attrclick事件上更改此设置,但没有任何反应。

Is this the right way to do it or is this possible?

这是正确的方法还是可能的?

回答by 26ph19

This will work

这将工作

$('input').on('click', function () {
   $(this).attr('type', 'password'); 
});

jsfiddle

提琴手

Update

更新

For IE read accepted answer for this post.

对于 IE,请阅读此帖子的已接受答案。

回答by trrrrrrm

jquery.Attrshould work the way that you want:

jquery.Attr应该按照您想要的方式工作:

http://jsfiddle.net/7UA95/

http://jsfiddle.net/7UA95/

UPDATE

更新

This one when you click on it:

当你点击它时:

http://jsfiddle.net/7UA95/1/

http://jsfiddle.net/7UA95/1/