如何解决有关输入的这些 Javascript 问题

How do I fix these Javascript problems about an input

提问人:BluedKong 提问时间:11/10/2023 最后编辑:SyferBluedKong 更新时间:11/10/2023 访问量:44

问:

所以我有这个密码页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="password.css">
    <title>Document</title>
</head>
<body>
    <div class="passwordquestion">
        <form>
            <label for="pswd">Enter password: </label>
            [<input type="password" id="pswd" maxlength="18">]
            <input type="button" value="Submit" id="LButton" onclick="checkPswd();" />
            
        </form>
    </div>
    <script type="text/javascript">
        function checkPswd() {
            var confirmPassword = "2pN7Ct24MNO56kc06";
            var password = document.getElementById("pswd").value;
            if (password == confirmPassword) {
                 window.location="index.html";
            }
            else if(password == "Pessword") {
                alert("Are you actually using that?")
            }
            else{
                alert("Failed Access");
            }
        }
    </script>
</body>
</html>

使用 CSS 文件

*{
    font-family: 'Courier New', Courier, monospace;
    background-color: black;
}
.passwordquestion{
    display: block;
    justify-content: center;
}

form{
    width: 31%;
    color: #00FF19;
    position: relative;
    margin: auto;
    margin-top: 24%;
    font-size: medium;
}
input{
    color: #00FF19;
    border: solid 1px black;
}

这是网站的图片(是的,它看起来像一个 Disney+ 泄漏

Here's the picture of the website in action(yes it looks like that one Disney+ leak

我面临的几个问题是:

1,与其按“提交”按钮提交,不如将其更改为仅按回车键(如果您输入密码并按回车键,它将删除整个输入的密码,我认为它基本上会重置整个网络)并删除该按钮

2,如果你点击输入框,框的轮廓就会出现,我还在想办法让输入框的轮廓消失

无论谁做的,我都很感激,兄弟,提前谢谢你

对于第二个问题,我没有尝试过任何事情,但是我尝试在按钮上放置一个奇怪的类型函数,但它不起作用:skull:

JavaScript HTML 按钮 输入 密码

评论


答:

0赞 user17758804 11/10/2023 #1

要解决密码 .css 文件中框中的大纲问题,请添加原始输入 css 设置并添加另一个选项,例如,当您聚焦输入时,它也会删除大纲outline: none

input:focus {
outline: none;
}

要回答有关使用 Enter 键输入密码的其他问题,请删除您的按钮,并在您的 JavaScript 代码中用于检查何时提交输入以及何时按下 Enter 键。addEventListener()

你已经有一个名为 checkPswd() 的函数,所以你可以使用类似

document.getElementById("passwordForm").addEventListener("submit", function(event) {
        event.preventDefault()
        checkPswd();
    }) // change your form id in index.html to "passwordForm"
document.getElementById("pswd").addEventListener("keyup", function(event) {
        if (event.key === "Enter") {
            event.preventDefault();
            checkPswd();
        }
    });

在代码的第一部分,我们正在侦听 id “passwordForm”(在 index.html 中将其更改为 this for your form ),以便提交。提交表单后,我们使用,我们阻止输入使用默认表单提交。然后我们运行你的函数,它与第二段代码类似,但它检查你的标签并等待 Enter 键的键启动,然后运行其他代码preventDefault()

评论

0赞 BluedKong 11/11/2023
嘿,伙计,谢谢你的回答。但我不太明白“将索引 .html 中的表单 ID 更改为”密码形式“部分的部分。 你介意帮我吗?
0赞 user17758804 11/12/2023
下面它说只需将其更改为<div class="passwordquestion"><form><form id=passwordForm>