我需要帮助在html/javascript创建一个密码表单

i need help creating a password form in html/javascript

本文关键字:一个 表单 密码 创建 javascript 帮助 html      更新时间:2023-09-26

我试图使一个密码字段,它不工作,如果有人知道什么是错误的
我将非常感谢我的代码(可能是所有的代码,因为这是我的第二个项目)。

    <!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Password</title>
        <script>
            (function pw_check() {
  var $password;
  $password = document.write(document.getElementById("password"));
  if ($password = "pass") {window.open("alert.html");} 
  else {window.open("pwfail.html");};
  });
        </script>
        <meta name="viewport" content="width=device-width; initial-scale=1.0">
    </head>
    <body>
        <form>
            <input type="password"/>
            <button onclick="pw_check" id="password">submit</button>
        </form>
    </body>
</html>

两个问题:

  1. onclick="pw_check();",否则你只是引用函数而不是实际调用它

  2. 你的pw_check()功能是各种错误。试一试:

    function pw_check() {
        var pw = document.getElementById('password_input').value;
        if( pw == "pass") location.href = "alert.html";
        else location.href = "pwfail.html";
    }
    

    然后把你的表单改成:

    <form>
        <input type="password" id="password_input" />
        <button type="button" onclick="pw_check();">submit</button>
    </form>
    

添加ID:

您尝试通过ID获得元素:document.getElementById("password"),而您的输入没有ID:

 <input type="password"/>

添加ID:

 <input id="password" type="password"/>

删除按钮的ID - ID必须是唯一的