0

入力フィールドに「パスワード」としてデフォルトのテキストがあり、キーが押されたときに入力タイプ「パスワード」に変更したい。しかし、これを試みると、入力は最初のキー押下を登録しませんが、入力タイプの切り替え後にすべてのキー押下を登録します。

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')"
 />
4

3 に答える 3

0

例を示します:</p>

<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 でテストされています。

于 2013-05-28T06:27:13.043 に答える
0

さまざまな方法で行うことができます。その一部をここにリストします。私があなたを助けてくれることを願っています

次のようなフィールドがあるとしましょう-

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

方法 1:

onClick でも、Jquery を使用して置き換えることができます。

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

方法 2:

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

(注: これは jquery 関数が古い IE で発行される可能性があるので注意してください)

方法 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">
于 2013-05-28T06:21:03.417 に答える