简体   繁体   English

以编程方式将文本框类型从 type='text' 更改为 type='password'

[英]programatically change textbox type from type='text' to type='password'

i have username and password textboxes.我有用户名和密码文本框。 i want them to both show the default text, ie "username" and "password", but when the user clicks on the password box it should dynamically change to a "password" type for security reasons.我希望它们都显示默认文本,即“用户名”和“密码”,但是当用户单击密码框时,出于安全原因,它应该动态更改为“密码”类型。

i've managed to do this easily enough with javascript code but it doesn't want to work in IE.我已经设法用 javascript 代码很容易地做到这一点,但它不想在 IE 中工作。

<input id="username" class="username" name="u" type="text" value="username" onclick="if(this.value='username') {this.value=''};" />
<input id="password" class="password" name="p" type="text" value="password" onclick="if(this.value='password') {this.value=''; this.type='password'};" /> 

Let say your html code is like this假设您的 html 代码是这样的

<input type="text" id="txtId" />

you can replace it by using Jquery,您可以使用 Jquery 替换它,

$('#txtId').replaceWith('<input type="password" id="txtId" />')

IE only lets you set the type of an input element once, when the element is created. IE 只允许您在创建元素时设置一次input元素的类型。 Attempts to set it after that will fail.之后尝试设置它会失败。

If you want to "change" the element in IE, what you'll likely need to do is create another element with the same attributes (except for the type, of course), and replace the existing element with it.如果您想在 IE 中“更改”元素,您可能需要做的是创建另一个具有相同属性的元素(当然类型除外),并用它替换现有元素。

Or create a dummy textbox for the password, and have the real password box hidden -- on focus the dummy box should hide itself, and show and focus on the real password box.或者为密码创建一个虚拟文本框,并隐藏真正的密码框——在焦点上,虚拟框应该隐藏自己,并显示并关注真正的密码框。

Would you consider using JQuery?您会考虑使用 JQuery 吗?

$("[name=fieldname]").attr("type", "password");

This worked for me Put this in the head section这对我有用 把它放在头部

function changeBack()
 {
     if(document.getElementById('smsl_pw').value=='')
     {
        document.getElementById('smsl_pw').type='text';
        document.getElementById('smsl_pw').value='Password';

     }

 }

 function changePass()
 {
    document.getElementById('smsl_pw').type='password'; 
    document.getElementById('smsl_pw').value='';

 }

Here is the input field <input id="smsl_pw" onclick="changePass();" onblur="changeBack();" name="smsl_pw" type="text" value="Password">这是输入字段<input id="smsl_pw" onclick="changePass();" onblur="changeBack();" name="smsl_pw" type="text" value="Password"> <input id="smsl_pw" onclick="changePass();" onblur="changeBack();" name="smsl_pw" type="text" value="Password">

Hope it helps someone希望它可以帮助某人

Here is a function to replace any form element by a password with the same value, knowing its id:这是一个 function 用具有相同值的密码替换任何表单元素,知道它的 id:

function replaceWithPassword(id)
{
    var o = $('input#' + id + ', textarea#'+id + ', select#'+id);
    o.replaceWith('<input type="password" value="' + o.val() + '" id="' + id + '" />');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM