使用JavaScript确认密码

使用JavaScript确认密码,javascript,html,Javascript,Html,我需要帮助制作网页 当用户键入密码时,我希望在我的部分中出现“密码不相同”的语句。 但我不明白我的代码出了什么问题 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Login Page </title> </head> <body> <f

我需要帮助制作网页

当用户键入密码时,我希望在我的部分中出现“密码不相同”的语句。 但我不明白我的代码出了什么问题

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> Login Page </title>
  </head>
  <body>
    <form action="login_2.php" method="post">
      <input type="hidden" name="action" value="login">
      <input type="hidden" name="hide" value="">
      <table class='center'>
        <tr><td>Login ID:</td><td><input type="text" name="ID"></td></tr>
        <tr><td>Password:</td><td><input type="password" name="password"></td></tr><br>
        <tr><td>confirm_password: </td><td><input type="password" name="confirm_password" onkeyup="test();"></td></tr><br>
        <tr><td><div id="pwd" style="height: 35px;"></div></td></tr>
        <tr><td>Full Name: </td><td><input type="text" name="name"></td></tr>
      </form>
      <script>
        function test(){
          if(document.info.password.value != document.info.confirm_password.value){
            document.getElementById('pwd').innerHTML='wrong';
          }else{
            document.getElementbyId('pwd').innerHTML='continue';
          }
        }
      </script>
    </table>
    <tr><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr>
  </body>
</html>

登录页面
登录ID:
密码:
确认密码:
全名: 功能测试(){ if(document.info.password.value!=document.info.confirm\u password.value){ document.getElementById('pwd').innerHTML='Error'; }否则{ document.getElementbyId('pwd').innerHTML='continue'; } }
由于输入字段上没有id,您可以将document.info更改为document.getElementsByName

function test(){
    if(document.getElementsByName("password")[0].value != document.getElementById("confirm_password")[0].value){
            document.getElementById('pwd').innerHTML='wrong';
    }else{
            document.getElementbyId('pwd').innerHTML='continue';
    }
}
在电视上看

jquery:

$(document).ready(function(){
$("input[name=confirm_password]").keyup(function(){
    pass=$("input[name=password]").val();
    confpass=$("input[name=confirm_password]").val();
    if(pass==confpass)
        $("#pwd").html("same");
    else
        $("#pwd").html("wrong");
});

}))

在表单信息中写下姓名,可以吗

   <script>
    function test(){
            if(document.info.password.value != document.info.confirm_password.value){
                alert( "password doesn't match!" );
        document.info.Name.focus() ;

        return false; 
            }else{  

         return true; 
            }
    }
    </script>

功能测试(){
if(document.info.password.value!=document.info.confirm\u password.value){
警报(“密码不匹配!”);
document.info.Name.focus();
返回false;
}否则{
返回true;
}
}

document.info
很可能会导致错误,因为
文档上没有
info
。您的
html
格式不正确。。。在关闭表格(
)之前,请先关闭表单(
),然后在
文档之后有一个
。getElementsByName(“确认密码”)[0]。值
在输入中添加ID将使您的工作更轻松。谢谢!问题是没有命名表单!:)您只犯了一个错误,您正在使用document.info.password.value在表单中写入名称