当我写这样做时,文本字段不会被填充

时间:2012-03-25 12:09:27

标签: javascript html textfield

以下是一个脚本,用于在单击它时清空用户名文本字段和密码文本字段。我想如果用户开始使用用户名字段填充密码字段为空,则用户名字段应填充其默认值,密码字段也是如此。虽然文本字段在点击时被清空但是如果用户继续填充下一个字段而没有填充值,则该时刻将另一个字段留空。有什么问题?

<script type="text/javascript">
function emptyTextField() {
    document.getElementById("username").value="";
            if( document.getElementById("password").value =="" ) {
                document.getElementById("password").value == "password";
            }
}

function emptyPasswordField() {
    document.getElementById("password").value="";
            if( document.getElementById("username").value == "" ) {
                document.getElementById("username").value == "Username or Email";
            }
}

HTML code:

<ul>  <form method="post" action="#">
          <li> <input type="text" id="username" value="Username or Email" size="25" name="UserID"  onfocus="emptyTextField()" /> </li>
          <li> <input type="password" id="password" value="password" size="25" name="UserPassword" onfocus="emptyPasswordField()" /> 
          <center><input type="submit" value="sign-in" style="font-size:20px" /> </center>
          </li>
              </form>
        </ul>

1 个答案:

答案 0 :(得分:1)

你犯了一个错误:

function emptyTextField() {
    document.getElementById("username").value="";
            if( document.getElementById("password").value =="" ) {
                document.getElementById("password").value == "password";
            }
}

function emptyPasswordField() {
    document.getElementById("password").value="";
            if( document.getElementById("username").value == "" ) {
                document.getElementById("username").value == "Username or Email";
            }
}

function emptyTextField() {
    document.getElementById("username").value="";
            if( document.getElementById("password").value =="" ) {
                document.getElementById("password").value = "password";
            }
}

function emptyPasswordField() {
    document.getElementById("password").value="";
            if( document.getElementById("username").value == "" ) {
                document.getElementById("username").value = "Username or Email";
            }
}

= = =。