使用jQuery将HTML文本输入更改为密码输入

发布于 2024-12-01 21:53:00 字数 737 浏览 0 评论 0原文

我有一个文本输入,其默认值“密码”。

当用户单击此字段时,即当焦点设置到此文本字段时,我想将输入类型更改为密码。

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小苏打饼 2024-12-08 21:53:00

更好的解决方案是使用 placeholder 属性将完全实现您想要的效果,而无需担心更改输入框的类型。

否则,您将希望使用以下方法将该属性作为属性而不是属性来寻址:

.prop("type", "password");

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:

.prop("type", "password");
清风挽心 2024-12-08 21:53:00

看看HTML5的placeholder属性,就可以实现这样的效果。

<input type="password" placeholder="Password" />

不过,如果您想支持旧版 IE,您可能需要考虑后备方案。

Have a look at the HTML5 placeholder attribute, which can achieve this effect.

<input type="password" placeholder="Password" />

You may have to look into fallbacks if you want to support old IE, though.

一曲爱恨情仇 2024-12-08 21:53:00

这不是直接可能的。 jQuery 甚至有自己的内置错误消息:

jQuery.error( "type property can't be changed" );

相反,您可以拥有另一个位置和大小完全相同的元素(密码 one)。输入是可见的,而密码最初是不可见的。然后,您可以在它们之间切换:

$('#input, #password').toggle();

但请确保将密码设为焦点。

http://jsfiddle.net/pimvdb/mqq4z/

That's not directly possible. jQuery even has its own built-in error message for that:

jQuery.error( "type property can't be changed" );

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:

$('#input, #password').toggle();

Make sure to focus the password one though.

http://jsfiddle.net/pimvdb/mqq4z/

千笙结 2024-12-08 21:53:00

您无法使用 jQuery 更改 input 元素的 type。原因是它在 Internet Explorer 中是不可能的,因此 jQuery 阻止您在任何浏览器中执行此操作(因为它旨在提高跨浏览器兼容性等)。

查看 .attr() 文档

注意: jQuery 禁止更改或的类型属性
元素并会在所有浏览器中抛出错误。这是
因为类型属性无法在 Internet Explorer 中更改。

解决您的问题的一种可能的解决方案可能是将文本(在 spandiv 或您真正喜欢的任何内容中)覆盖到 type="password"< /code> 输入,并隐藏焦点上的文本。

You can't change the type of an input 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:

Note: jQuery prohibits changing the type attribute on an or
element and will throw an error in all browsers. This is
because the type attribute cannot be changed in Internet Explorer.

One potential solution to your problem could be to overlay the text (in a span or a div or whatever you like really) onto a type="password" input, and hide the text on focus.

葮薆情 2024-12-08 21:53:00

使用 HTML5 占位符属性将实现这一点。例如:

<input type = "password" placeholder = "Password" />

需要一个 javascript / jQuery 解决方案来支持不支持 HTML5 的客户端。

Using the HTML5 placeholder attribute will accomplish this. For example:

<input type = "password" placeholder = "Password" />

A javascript / jQuery solution will be needed to support clients not supporting HTML5.

暖树树初阳… 2024-12-08 21:53:00

正如建议的,您应该使用 placeholder 和插件来使其在 IE 中工作。话虽这么说,如果您仍然想更改 input 的类型并且不需要支持 IE,则可以使用 prop jQuery 中的方法:

$("#passwordtxt").prop("type", "password");

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 an input and don't need to support IE, you can do it using the prop method in jQuery:

$("#passwordtxt").prop("type", "password");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文