簡體   English   中英

如何在沒有按鍵延遲的情況下將輸入類型更改為密碼

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

好的,我在輸入字段中有默認文本作為“ password”,當我按下一個鍵時,我希望它更改為輸入類型“ password”。 但是,當我嘗試執行此操作時,輸入不會注冊我的第一個按鍵,但是會在輸入類型切換之后注冊所有的按鍵。

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通過占位符屬性為我們解決了這個問題。 無需再次管理活動...請檢查以下內容

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

您可以用不同的方式來做,這里列出了其中的一些! 希望我能幫助你

假設您有喜歡的領域-

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

方法1:

您甚至可以在o​​nClick上使用Jquery替換它,

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

方式2:

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

(注意:這是舊的IE中可能會發出的jquery函數,請謹慎操作)

方法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

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

舉個例子:

<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()">

此代碼已在google chrome中經過測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM