1

我想创建一个带有可隐藏和可显示密码输入的注册表单。我试过这个(JS):

function showhide(melyik,hol) {
    var melyiket = document.getElementById(melyik);
    var melyikkel = document.getElementById(hol);

    if (melyikkel.value == '1') {
        document.getElementById(melyikkel).value='0';   
        document.getElementById(melyiket).type='password'; 
    } else {
        document.getElementById(melyikkel).value='1';   
        document.getElementById(melyiket).type='text';                 
    } 
}

形式:

<div style="margin-top:20px;">
    <p>SQL host: <input type="text" name="sql_host" value="localhost"/></p>
    <p>SQL adatbázis: <input type="text" name="sql_db"/></p>
    <p>SQL felhasználó: <input type="text" name="sql_user"/></p>
    <p>SQL jelszó: <input type="text" name="sql_password" id="sql_password"/>
        <input type="checkbox" name="show_password_1" id="show_password_1" value="0" onclick="showhide('sql_password','show_password_1');">
    </p>
</div>

我想这样做:如果选中复选框,则密码输入类型为文本。未选中时,密码类型为密码...我编写了此 JavaScript,但无法正常工作。:-(

4

1 回答 1

2

用于.checked查看复选框是否被选中,而不是值。这是一个工作示例

HTML:

<input type="password" name="pwd" id="pwd" />
<input type="checkbox" name="show" id="show" onclick="showhide('pwd','show');">​

JS:

function showhide(pwd,show)
{
    var epwd = document.getElementById(pwd);
    var eshow = document.getElementById(show);

    epwd.type = eshow.checked ? 'text' : 'password';                 

}​
于 2013-01-03T13:36:19.287 回答