简体   繁体   English

如何在没有按键延迟的情况下将输入类型更改为密码

[英]How to change input type to password without key press delay

ok I have default text in an input field as "password" and when a key is pressed I want it to change to the input type "password". 好的,我在输入字段中有默认文本作为“ password”,当我按下一个键时,我希望它更改为输入类型“ password”。 But when I attempt this the input doesn't register my first key press but it registers all key presses after the input type switch. 但是,当我尝试执行此操作时,输入不会注册我的第一个按键,但是会在输入类型切换之后注册所有的按键。

function inputField(focus, inputValue, inputID){
// change inputID variable into pointer to the actual ID
var iD = document.getElementById(inputID);

// check if input has focus and handle default value changes, password field type changes, and font color changes.
if (focus == "on"){

    if(iD.value == inputValue){
        iD.setSelectionRange(0, 0);
        iD.style.color = "#b2b2b2";
    }
    iD.onkeypress = function(){
        if(iD.value == "password" || iD.value == "retype password"){
            iD.type = "password";
        }
        if (iD.value != "" && iD.value == inputValue){
                iD.value = "";
                iD.style.color = "#000000";
        }
    }
}else if(focus == "off"){
    if (iD.value == ""){
        if(iD.type == "password"){
            iD.type = "text";
        }
        iD.style.color = "#787878";
        iD.value = inputValue;
    }else if(iD.value == inputValue){
        iD.style.color = "#787878"
    }
}
}

<input
 id = "registerPassword"
 class = "loginSectionInput"
     type = "text"
 name = "rPassword"
 value = "password"
 onfocus = "inputField('on', 'password', this.id)"
 onblur = "inputField('off', 'password', this.id)"
 onchange = "formCheck('registerPassword')"
 />

HTML5 solves this problem for us with the placeholder attribute. HTML5通过占位符属性为我们解决了这个问题。 No need to manage the events again...Please check some like following 无需再次管理活动...请检查以下内容

<input type=password name="pwd" placeholder="Password">

You can do it in different ways, some of them are listed here! 您可以用不同的方式来做,这里列出了其中的一些! hope i have help you 希望我能帮助你

Let say you have field like- 假设您有喜欢的领域-

<input type="text" id="mypswfield" name="mypswfield" />

Way 1: 方法1:

On onClick even, you can replace it by using Jquery, 您甚至可以在o​​nClick上使用Jquery替换它,

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

Way 2: 方式2:

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

(Note: This is jquery function may issue in old IE so careful) (注意:这是旧的IE中可能会发出的jquery函数,请谨慎操作)

Way 3: Create function 方法3:创建功能

function txtField()
{
     if(document.getElementById('mypswfield').value=='')
     {
        document.getElementById('mypswfield').type='text';
        document.getElementById('mypswfield').value='Password';

     }

}
function txtPawd()
{
    document.getElementById('mypswfield').type='password'; 
    document.getElementById('mypswfield').value='';

}

HTML 的HTML

<input id="mypswfield" onclick="txtField();" onblur="txtPawd();" name="mypswfield" type="text" value="Password">

show you an example: 举个例子:

<html><head><script>function swithch(){
  alert("myText is ");
    var myText=document.getElementById("myId");
    alert("myText is "+myText);
    alert("myText is type "+myText.type);
    alert("myText is  value "+myText.value);
    myText.type='text';
    myText.value='56789';
  }</script></head><body>
 TT <input type="password" name="myText" id="myId" value="123456"/> <input type="button" name="switch" value="swithch" onclick="swithch()">

this code have been tested at google chrome. 此代码已在google chrome中经过测试。

暂无
暂无

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

相关问题 如何在输入字段中无按键延迟地播放声音? - How to play a sound on key press in input field without delay? 如何通过Javascript更改输入[input = type]的值? - How to change value of input[type=number] on key press via Javascript? 当输入类型=密码时,如何确保立即进行值屏蔽,而不会在电话上造成延迟? - How to ensure value masking happens immediately when input type = password, without delay on a phone? 按键之间无延迟 - No delay between key press 当用户在html的密码字段中输入字符串时,如何在将文本更改为密码之前延迟(大约0.5秒) - How to delay (about 0.5 second) before change text to password when user type a string into password field in html 如何在Enter按下时触发HTML密码输入 - How to trigger HTML password input on enter press 在移动模式下如何处理按键事件。 当在输入类型=“文本”上按下键时..在桌面模式下正常工作(通过键盘按下键) - how to handle on key press event on mobile mode . when key press on input type=“text”.. its work proper on desktop mode (key press by keyboard) 如何在不使用输入类型=“密码”的情况下将文本转换为* - how to convert text to * without any use of input type=“password” 删除 Javascript 中的按键延迟 - Remove key press delay in Javascript 如何将HTML输入类型属性从“密码”更改为“文本”? - How to change the HTML input type attribute from 'password' to' text'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM