密码生成器在我移动滑块之前不起作用

问题描述 投票:1回答:2

问题:只有移动滑块后,密码生成器才能工作。但是,我希望该值默认为8。因此,如果有人加载页面并单击生成PW,则会填充8的随机pw。

//generate a password function
function passwordGenerator () {

// Length of the password?
var passwordLength = document.getElementById('num').value;


// characters options for PW
const values = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";


// defining password

var password = "";


// creating a loop to choose password

for (var i = 1; i <= passwordLength; i++) {
    password = password + values.charAt(Math.floor(Math.random() * Math.floor(values.length -1)));
}
  
// adding the password to the content area
document.getElementById('display').value = password;
}

// adjust value when moving slider
function sliderMove(){
 document.getElementById('num').value = document.getElementById('slider1').value;
 document.getElementById('num').textContent = document.getElementById('num').value;
}

//copy to clipboard
function selectText() {
  const input = document.getElementById('display');
  input.focus();
  input.select();
  document.execCommand('copy')
}
.backgroundWhite {

    background-color: white;
    border: darkgray 2px solid;
    padding-bottom: 25px;
}


.backgroundGray {
    background-color: lightgray;
    width: 100%;
    height: 500%;
}

.passwordBox {

    width: 500px;
    height: 200px;
    text-align: center;
}


body {
    height: 100%;
    background-color: lightgray;
}

.headText {
    padding: 50px;
}

.buttonOnClick {
    margin: 20px;
}
.passGenButton {
    color: white;
    background-color: red;
    margin-right: 15%;
    height: 40px;
    border-radius: 12px;
}
.copyButton {
    margin-left: 15%;
    background-color: darkgray;
    color: white;
    height: 40px;
    border-radius: 12px;
}

textarea {
padding: 20px;
font-size: 19px;
color: #4f4f4f;
}

.titleClass {

    padding-top: 10px;
}


@media (max-width: 537px) {
    .passGenButton {
        color: white;
        background-color: red;
        margin-right: 1%;
        height: 40px;
        border-radius: 12px;
    }
    .copyButton {
        margin-left: 1%;
        background-color: darkgray;
        color: white;
        height: 40px;
        border-radius: 12px;
    }
    .passwordBox {
        width: 400px;
        height: 200px;
        text-align: center;
    }
    .backgroundWhite {

        background-color: white;
        border: darkgray 2px solid;
        padding-bottom: 25px;
        padding-left: 20px;
    }

} 
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
        integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="app.js"></script>
    <title>Random Password Generator</title>
</head>

<body>
    <div class="conatiner backgroundGray">
        <div class="row">
            <div class="col-12">
                <div class="topText">
                    <!-- Header -->
                    <h1 class="text-center text-dark headText">Password Generator</h1>
                </div>

                <div class="container">
                    <div class='row'>
                        <div class="col-lg-12 col-sm-12 text-center">
                            <div class="content backgroundWhite">
                                <!-- Sub Header -->
                                <h4 class="titleClass">Generate a Password</h4>
                                <br />



                                <!-- Slider -->
                                <div class="slidecontainer">
                                        <p>Select PW Length</p>
                                    <input id="slider1" type="range" min="8" max="128" value="8" onchange="sliderMove()"
                                        class="robClass">
                                    <span id="num">8</span>
                                </div>
                                <br />

                                <!-- Password Box -->
                                <textarea class="passwordBox" type="text" id="display"
                                    placeholder="Your Secure Password"></textarea>
                                <br />
                                <button onclick="passwordGenerator()" class="passGenButton buttonOnClick">Generate
                                    Password</button>
                                <button class="buttonOnClick copyButton" defaultValue="8" onclick="selectText()">Copy to
                                    clipboard</button>
                                <div id='length'></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


</body>

</html>

我玩过HTML和JS。我试图创建一个var num = 8;没运气。

有人对我该怎么做有任何想法吗?

javascript html
2个回答
2
投票

passwordGenerator功能开始时:

var passwordLength = document.getElementById('num').value;

此元素(<span id="num">8</span>)没有value属性,因此您的函数将尝试生成长度等于undefined的密码。

您应该改为从滑块获取值:

var passwordLength = document.getElementById('slider1').value;

您也可以简化您的功能sliderMove功能:

function sliderMove() {
    document.getElementById('num').textContent = document.getElementById('slider1').value;
}

-1
投票

您可以只将sliderMove()放在.js文件的末尾以模拟网站首次加载时滑块的初始移动。


-1
投票

您可以只将sliderMove()放在.js文件的末尾以模拟网站首次加载时滑块的初始移动。

© www.soinside.com 2019 - 2024. All rights reserved.