使用jQuery将HTML文本输入更改为密码输入
我有一个文本输入,其默认值“密码”。
当用户单击此字段时,即当焦点设置到此文本字段时,我想将输入类型更改为密码。
我已经使用 jQuery 尝试过此操作,但没有喜悦:
$("#passwordtxt").click(function(e){
e.stopPropagation();
if($("#passwordtxt").val() == "Password"){
$("#passwordtxt").val("");
}
$("#passwordtxt").attr('type','password');
});
这是它适用于的 HTML:
<form name = "loginform" action = "get_login.php" method = "post">
<input id = "emailtxt" name = "username" type="text" size="25" value = "E-mail"/>
<input id = "passwordtxt" name = "password" type="text" size="25" value = "Password"/>
</form>
结果:它不会将类型更改为密码,它只是保留为文本......
I have a text input which has a default value "password".
When the user clicks this field, i.e. when the focus is set to this textfield, I would like to change the input type to password..
I have tried this using jQuery, but no joy:
$("#passwordtxt").click(function(e){
e.stopPropagation();
if($("#passwordtxt").val() == "Password"){
$("#passwordtxt").val("");
}
$("#passwordtxt").attr('type','password');
});
Here is the HTML it applies to:
<form name = "loginform" action = "get_login.php" method = "post">
<input id = "emailtxt" name = "username" type="text" size="25" value = "E-mail"/>
<input id = "passwordtxt" name = "password" type="text" size="25" value = "Password"/>
</form>
RESULT: It doesn't change the type to password, it just stays as text...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更好的解决方案是使用
placeholder
属性将完全实现您想要的效果,而无需担心更改输入框的类型。否则,您将希望使用以下方法将该属性作为属性而不是属性来寻址:
A better solution would be to use the
placeholder
property which will achieve exactly what you want without fussing over changing the type of the input box.otherwise you will want to address the property as a property and not as a attribute using:
看看HTML5的placeholder属性,就可以实现这样的效果。
不过,如果您想支持旧版 IE,您可能需要考虑后备方案。
Have a look at the HTML5 placeholder attribute, which can achieve this effect.
You may have to look into fallbacks if you want to support old IE, though.
这不是直接可能的。 jQuery 甚至有自己的内置错误消息:
相反,您可以拥有另一个位置和大小完全相同的元素(密码 one)。输入是可见的,而密码最初是不可见的。然后,您可以在它们之间切换:
但请确保将密码设为焦点。
http://jsfiddle.net/pimvdb/mqq4z/
That's not directly possible. jQuery even has its own built-in error message for that:
Instead, you could have another element (password one) with the exact same position and size. The input is visible, whereas the password is initially not. Then, you can toggle between them:
Make sure to focus the password one though.
http://jsfiddle.net/pimvdb/mqq4z/
您无法使用 jQuery 更改
input
元素的type
。原因是它在 Internet Explorer 中是不可能的,因此 jQuery 阻止您在任何浏览器中执行此操作(因为它旨在提高跨浏览器兼容性等)。查看
.attr()
文档:解决您的问题的一种可能的解决方案可能是将文本(在
span
或div
或您真正喜欢的任何内容中)覆盖到type="password"< /code> 输入,并隐藏
焦点
上的文本。You can't change the
type
of aninput
element with jQuery. The reason is that it's not possible in Internet Explorer, so jQuery prevents you from doing it in any browser (as it's meant to improve cross browser compatibility and all that).Have a look at the
.attr()
docs:One potential solution to your problem could be to overlay the text (in a
span
or adiv
or whatever you like really) onto atype="password"
input, and hide the text onfocus
.使用 HTML5
占位符
属性将实现这一点。例如:需要一个 javascript / jQuery 解决方案来支持不支持 HTML5 的客户端。
Using the HTML5
placeholder
attribute will accomplish this. For example:A javascript / jQuery solution will be needed to support clients not supporting HTML5.
正如建议的,您应该使用
placeholder
和插件来使其在 IE 中工作。话虽这么说,如果您仍然想更改input
的类型并且不需要支持 IE,则可以使用prop
jQuery 中的方法:As suggested, you should use
placeholder
and plugins to get it to work in IE. That being said, if you still want to change the type of aninput
and don't need to support IE, you can do it using theprop
method in jQuery: