自学内容网 自学内容网

js如何点击生成4位随机数

效果图:

代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generate Random Four-Digit Verification Code</title>
</head>
<body>
    <h1>点击生成四位随机数</h1>
    <input type="text" id="verificationInput">
    <button id="generateCode">Generate Code</button>

    <script>
        const generateCodeButton = document.getElementById("generateCode");
        const verificationInput = document.getElementById("verificationInput");
        let randomCode = '';

        generateCodeButton.addEventListener('click', function() {
            randomCode = Math.floor(1000 + Math.random() * 9000); // Generate a random four-digit number
            verificationInput.value = randomCode;
        });

        verificationInput.addEventListener('click', function() {
            verificationInput.value = randomCode;
        });
    </script>
</body>
</html>

当前效果是需要点击按钮,也可以写在点击按钮里面,根据需求适当调整;

希望可以帮到你们;


原文地址:https://blog.csdn.net/tianxianghuiwei/article/details/138229213

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!