document.addEventListener('DOMContentLoaded', function() { // 获取按钮和输入框的元素 const generateBtn = document.getElementById('10'); const phoneInput = document.getElementById('7'); const verifyInput = document.getElementById('8'); const submitBtn = document.getElementById('submit-btn'); let randomNum = null; // 存储随机数的变量 // 生成随机数并弹窗显示 generateBtn.addEventListener('click', function(event) { event.preventDefault(); // 如果手机号不满足要求,则弹出手机号错误的提示框 if (!validatePhone()) { alert('手机号错误'); return; } // 如果随机数还未生成,则生成新的随机数 if (randomNum === null) { randomNum = Math.floor(Math.random() * 10000); } // 弹窗显示随机数 alert('验证码:' + randomNum); }); // 提交按钮点击事件监听器 submitBtn.addEventListener('click', function(event) { // 获取存储的随机数 const storedRandomNum = randomNum; // 获取用户输入的验证码和手机号 const userEnteredNum = parseInt(verifyInput.value); const phoneValue = phoneInput.value; // 检查是否已生成随机数 if (storedRandomNum === null) { event.preventDefault(); } else if (userEnteredNum !== storedRandomNum) { // 检查验证码是否正确 event.preventDefault(); alert('验证码错误'); } else { // 验证码正确,提交表单 // 这里可以添加其他逻辑或调用其他函数来处理表单提交 } }); // 手机号验证函数 function validatePhone() { const phoneValue = phoneInput.value; // 检查手机号是否为11个数字 return /^\d{11}$/.test(phoneValue); } }); 为什么刚进入界面时会弹出”手机号错误“的弹窗

时间: 2024-04-15 19:24:51 浏览: 21
刚进入界面时会弹出"手机号错误"的弹窗,是因为在点击生成随机数按钮之前,会先调用手机号验证函数 validatePhone() 来检查手机号是否满足要求。 如果手机号不满足要求,即不是11个数字,那么就会触发 alert('手机号错误'),弹出"手机号错误"的提示框。 这样做是为了确保用户输入的手机号是有效的,只有在手机号满足要求时才能生成随机数。
相关问题

document.addEventListener('DOMContentLoaded', function() { //获取按钮和输入框的元素 const generateBtn = document.getElementById('10'); const phoneInput = document.getElementById('7'); const verifyInput = document.getElementById('8'); const submitBtn = document.getElementById('submit-btn'); let randomNum = null; //存储随机数的变量 //生成随机数并弹窗显示 generateBtn.addEventListener('click', function(event) { event.preventDefault(); //如果手机号不满足要求,则弹出手机号错误的提示框 if (!validatePhone()) { alert('手机号错误'); return; } //如果随机数还未生成,则生成新的随机数 if (randomNum === null) { randomNum = Math.floor(Math.random() * 10000); } //弹窗显示随机数 alert('验证码:' + randomNum); }); //提交按钮点击事件监听器 submitBtn.addEventListener('click', function(event) { //获取存储的随机数 const storedRandomNum = randomNum; //获取用户输入的验证码和手机号 const userEnteredNum = parseInt(verifyInput.value); const phoneValue = phoneInput.value; //检查是否已生成随机数 if (storedRandomNum === null) { event.preventDefault(); } else if (userEnteredNum !== storedRandomNum) { //检查验证码是否正确 event.preventDefault(); alert('验证码错误'); } else { //验证码正确,提交表单 //这里可以添加其他逻辑或调用其他函数来处理表单提交 } }); //手机号验证函数 function validatePhone() { const phoneValue = phoneInput.value; //检查手机号是否为11个数字 return /^\d{11}$/.test(phoneValue); } });

刚进入界面时不应该弹出"手机号错误"的弹窗,因为此时用户还没有进行任何操作。可能的原因是在页面加载完成后,自动触发了生成随机数按钮的点击事件。 请检查页面加载完成后是否存在其他代码或事件触发了生成随机数按钮的点击事件。可以通过在生成随机数按钮的点击事件处理函数中添加 console.log() 或者 alert() 来进行调试,查看是否在页面加载完成时就执行了生成随机数按钮的点击事件。 另外,也要确保生成随机数按钮的相关元素(如id为 '10')存在于页面中,并且与对应的按钮元素匹配。

const closeBtn = document.getElementById("closeVideoBtn"); // 加载视频 function loadVideo() { const videoSource = "../assets/videos/1、乡镇(街道)平安指数.mp4"; videoElement.src = videoSource; console.log(1); } // 关闭视频 function closeVideo() { videoElement.pause(); document.getElementById("video-container").hidden = true; } // 监听关闭按钮点击事件 closeBtn.addEventListener("click", closeVideo); Cannot read properties of null (reading 'addEventListener')

当报错 `Cannot read properties of null (reading 'addEventListener')` 出现时,通常是因为 `document.getElementById("closeVideoBtn")` 没有找到对应的元素,返回了 `null`。这可能是由于以下原因导致的: 1. HTML 中没有具有 `id="closeVideoBtn"` 的元素。 2. JavaScript 代码在 HTML 元素加载之前执行,导致无法找到元素。 请确保以下几点: 1. 确认 HTML 中存在具有 `id="closeVideoBtn"` 的元素,并且该元素在 JavaScript 代码执行时已经被加载到 DOM 中。 ```html <button id="closeVideoBtn">关闭视频</button> ``` 2. 将 JavaScript 代码放置在 HTML 元素之后,或者确保代码在页面加载完成后执行。可以将代码放在以下位置: - 将 `<script>` 标签放置在 HTML 元素之后。 - 将代码放置在 `DOMContentLoaded` 事件监听器内部,以确保代码在页面加载完成后执行。 ```javascript document.addEventListener("DOMContentLoaded", function() { // 在此处放置 JavaScript 代码 }); ``` 请检查上述两个方面是否满足,以确保正确获取到 `closeBtn` 元素,并添加事件监听器。

相关推荐

document.addEventListener('DOMContentLoaded', function() { const form = document.getElementById('registration-form'); form.addEventListener('submit', function (event) { event.preventDefault(); // 阻止表单提交 const name = document.getElementById('name').value; const password = document.getElementById('password').value; const email = document.getElementById('email').value; console.log('注册信息:'); console.log('用户名:', name); console.log('密码:', password); console.log('邮箱:', email); }); }); const express = require('express'); const bodyParser = require('body-parser'); const mssql = require('mssql'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); // 创建与数据库的连接 const connection = mssql.createConnection({ server: '192.168.10.223', user: 'sa', password: 'Servo01', database: 'demo' }); // 连接数据库 connection.connect(function (err) { if (err) { console.error('Error connecting to database:', err); return; } console.log('Connected to database!'); }); // 处理注册页面的 POST 请求 app.post('/', function (req, res) { const name = req.body.name; const password = req.body.password; const email = req.body.email; console.log('注册信息:'); console.log('用户名:', name); console.log('密码:', password); console.log('邮箱:', email); // 将数据保存到数据库 const query = INSERT INTO username (name, password, email) VALUES ('${name}','${password}','${email}'); const request = new mssql.Request(); connection.query(query, function (err, res) { if (err) { console.error('Error saving data to database:', err); return; } console.log('数据已保存到数据库!'); }); res.send('注册成功!'); }); app.listen(3000, function () { console.log('Server is running on port 3000'); });我要连接到sqlsever数据库然后将注册信息导进username表中

document.addEventListener('DOMContentLoaded', function () { const form = document.getElementById('registration-form'); form.addEventListener('submit', function (event) { event.preventDefault(); // 阻止表单提交 const name = document.getElementById('name').value; const password = document.getElementById('password').value; const email = document.getElementById('email').value; console.log('注册信息:'); console.log('用户名:', name); console.log('密码:', password); console.log('邮箱:', email); }); }); const express = require('express'); const bodyParser = require('body-parser'); const mssql = require('mssql'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); // 创建与数据库的连接 const connection = mssql.createConnection({ server: '192.168.10.223', user: 'sa', password: 'Servo01', database: 'demo' }); // 连接数据库 connection.connect(function (err) { if (err) { console.error('Error connecting to database:', err); return; } console.log('Connected to database!'); }); // 处理注册页面的 POST 请求 app.post('/', function (req, res) { const name = req.body.name; const password = req.body.password; const email = req.body.email; console.log('注册信息:'); console.log('用户名:', name); console.log('密码:', password); console.log('邮箱:', email); // 将数据保存到数据库 const query = INSERT INTO username (name, password, email) VALUES ('${name}','${password}','${email}'); const request = new mssql.Request(); request.query(query, function (err, request) { if (err) { console.error('Error saving data to database:', err); return; } console.log('数据已保存到数据库!'); }); res.send('注册成功!'); }); app.listen(3000, function () { console.log('Server is running on port 3000'); });注册页面无法进入sqlsever的数据库

<!DOCTYPE html> <html> <head> <title>注册页面</title> </head> <body> 用户注册 <form method="post" action="http://localhost:3000" id="registration-form"> <label for="username">用户名:</label> <input type="text" id="name" name="name" placeholder="请输入用户名" pattern="^[a-zA-Z0-9_]{3,20}$" required> <label for="email">邮箱:</label> <input type="email" id="email" name="email" placeholder="请输入邮箱地址" required> <label for="password">密码:</label> <input type="password" id="password" name="password" placeholder="请输入密码"pattern="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$" required> <input type="submit" value="注册"> </form> <script src="../zhuce/zhuce1.js"></script> </body> </html> const form = document.getElementById('register-form'); form.addEventListener('submit', function (event) { event.preventDefault(); // 阻止表单提交 const name = document.getElementById('name').value; const password = document.getElementById('password').value; const email = document.getElementById('email').value; console.log('注册信息:'); console.log('用户名:', name); console.log('密码:', password); console.log('邮箱:', email); }); const express = require('express'); const bodyParser = require('body-parser'); const mssql = require('mssql'); // 假设使用 MySQL 数据库 const app = express(); app.use(bodyParser.urlencoded({ extended: false })); // 创建与数据库的连接 const connection = mssql.createConnection({ server: 'CRMTEST', user: 'sa', password: 'Servo01', database: 'demo' }); // 连接数据库 connection.connect(function (err) { if (err) { console.error('Error connecting to database:', err); return; } console.log('Connected to database!'); }); // 处理注册页面的 POST 请求 app.post('http://localhost:3000', function (req, res) { const name = req.body.name; const password = req.body.password; const email = req.body.email; console.log('注册信息:'); console.log('用户名:', name); console.log('密码:', password); console.log('邮箱:', email); // 将数据保存到数据库 const query = INSERT INTO username (name, password, email) VALUES (${name},${name}, ${name}); connection.query(query, [name, password, email], function (err, result) { if (err) { console.error('Error saving data to database:', err); return; } console.log('数据已保存到数据库!'); }); res.send('注册成功!'); }); app.listen(3000, function () { console.log('Server is running on port 3000'); });为什么控制台打印不出来

最新推荐

recommend-type

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx
recommend-type

华为OD机试D卷 - 机场航班调度程序 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

基于FPGA读取设计的心电图代码源码+全部资料齐全.zip

【资源说明】 基于FPGA读取设计的心电图代码源码+全部资料齐全.zip基于FPGA读取设计的心电图代码源码+全部资料齐全.zip 【备注】 1、该项目是高分课程设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11/linux测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip

【资源说明】 基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

华为OD机试D卷 - 数的分解 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。