search
HomeWeb Front-endJS TutorialAdd, delete, and modify HTML tables using JS

The requirements are as follows:

Write an html page with a table in it to store user information, including: user name, password, name, email, phone number, QQ, and ID number.
Now we need to dynamically add, delete, modify and check the table through js (just memory operations):
First, use js to load 3 initialization records when loading the page;
There is a button to add records, click A div layer pops up to provide input. Each field must conform to the input format and cannot be empty:
Username: English + numbers + underline;
Password: English + numbers + underline + 6 or more digits;
Name : Chinese;
Email, phone, QQ, ID number conform to the format;
Each record has modifications and deletions;
Modifications are similar to additions, and the original values ​​must be read out;

HTML page code:

<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>js增删改 v1.0</title>
 <script src="js/test.js" type="text/javascript" charset="utf-8"></script>
</head>
<body >
<center>
 <br/><br/>
 <h2 id="js增删改-nbsp-v">js增删改 v1.0</h2>
 <br/><br/>
 <a href="javascript:showAddInput();">添加数据</a>
 <br/><br/> 
<div class="table" >
 <table border="1" style="text-align:center" id="table">
 <tr>
  <th>用户名</th>
  <th>密码</th>
  <th>姓名</th>
  <th>邮箱</th>
  <th>电话</th>
  <th>qq</th>
  <th>身份证号</th>
  <th>操作</th>
 </tr>
 <tr>
  <td>A1</td>
  <td>123</td>
  <td>a</td>
  <td>a@qq.com</td>
  <td>123456789</td>
  <td>40040044</td>
  <td>270205197405213513</td>
  <td><a style="color:blue;cursor:pointer;" onclick="updateRow(this);" >修改<a> <a style="color:blue;cursor:pointer;" onclick="delRow(this);">删除</a></td>
 </tr>
 <tr>
  <td>A2</td>
  <td>456</td>
  <td>b</td>
  <td>b@qq.com</td>
  <td>987654321</td>
  <td>30030033</td>
  <td>470205197405213513 </td>
  <td><a style="color:blue;cursor:pointer;" onclick="updateRow(this);">修改<a> <a style="color:blue;cursor:pointer;" onclick="delRow(this);">删除</a></td>
 </tr>
 <tr>
  <td>A3</td>
  <td>789</td>
  <td>c</td>
  <td>c@qq.com</td>
  <td>800800820</td>
  <td>30030030</td>
  <td>570205197405213513 </td>
  <td><a style="color:blue;cursor:pointer;" onclick="updateRow(this);">修改<a> <a style="color:blue;cursor:pointer;" onclick="delRow(this);">删除</a></td>
 </tr>
 </table>
</div>
 
<div style="display:none" id="addinfo">
<form>
 <br>
 用户名:(用户名只能用英文+数字或下划线)<br><input type="text" id="username" /><br><!--限制用户名只能用英文 下划线 或数字-->
 密码:(英文+数字或下划线,长度不小于6)<br><input type="text" id="password"/><br>
 姓名:(只能是汉字)<br><input type="text" id="name"/><br>
 邮箱:<br><input type="text" id="email"/><br>
 电话:<br><input type="text" id="phone"/><br>
 qq:<br><input type="text" id="qq"/><br>
 身份证:<br><input type="text" id="uid"/><br><br>
 <input type="button" value="提交" onclick="addInfo()" id="btn_add">
 <input type="button" value="提交" onclick="updateInfo()" style="display:none" id="btn_update">
 <input type="button" value="取消" onclick="hideAddInput()">
</form>
</div>
</center>
</body>
</html>

js code:

var row = 0 ; //定义全局行数用于修改
var reg_email = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; //用于判断邮箱格式
var reg_name = /^((\w*\d\w*[a-z]\w*)|(\w*[a-z]\w*\d\w*))$/i; //用于判断用户名格式
var reg_chinese = /^[\u0391-\uFFE5]+$/ ; //用于判断姓名格式
var reg_pass = /^((\w*\d\w*[a-z]\w*)|(\w*[a-z]\w*\d\w*))$/i;//用于判断密码格式
//----获取行号-----
function getRow(r){
 var i=r.parentNode.parentNode.rowIndex; 
 return i ;
}
//----获取行号-----
 
//----删除某一行-----
function delRow(r){ 
 document.getElementById(&#39;table&#39;).deleteRow(getRow(r));
}
//----删除某一行-----
 
//----清除添加信息框的内容-----
function cleanAddInput(){
 document.getElementById(&#39;username&#39;).value=&#39;&#39;;
 document.getElementById(&#39;password&#39;).value=&#39;&#39;; 
 document.getElementById(&#39;name&#39;).value=&#39;&#39;;
 document.getElementById(&#39;email&#39;).value=&#39;&#39;;
 document.getElementById(&#39;phone&#39;).value=&#39;&#39;;
 document.getElementById(&#39;qq&#39;).value=&#39;&#39;;
 document.getElementById(&#39;uid&#39;).value=&#39;&#39;;
}
//----清除添加信息框的内容-----
 
//----显示添加信息框-----
function showAddInput(){
 document.getElementById(&#39;addinfo&#39;).style="display:block-inline" ;
 document.getElementById(&#39;btn_add&#39;).style="display:block-inline" ;
 document.getElementById(&#39;btn_update&#39;).style="display:none" ;
 cleanAddInput(); 
}
//----显示添加信息框-----
 
//----隐藏添加信息框-----
function hideAddInput(){
 document.getElementById(&#39;addinfo&#39;).style="display:none" ;
 
}
//----隐藏添加信息框-----
 
//----判断输入框的信息是否符合要求-----
function judge(){
 //根据id获取表单信息
 var username = document.getElementById(&#39;username&#39;).value;
 var password = document.getElementById(&#39;password&#39;).value; 
 var name = document.getElementById(&#39;name&#39;).value;
 var email = document.getElementById(&#39;email&#39;).value;
 var phone = document.getElementById(&#39;phone&#39;).value;
 var qq = document.getElementById(&#39;qq&#39;).value;
 var uid = document.getElementById(&#39;uid&#39;).value;
 var judge = true ; //用于判断表单信息是否符合
 if(username==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入用户名&#39;);
 }else if(password==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入密码&#39;);
 }else if(name==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入姓名&#39;);
 }else if(email==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入邮箱&#39;);
 }else if(phone==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入电话&#39;);
 }else if(qq==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入qq&#39;);
 }else if(uid==&#39;&#39;){
  judge = false ;
  alert(&#39;请输入身份证&#39;);
 }else if(uid.length!=18){
  judge = false ;
  alert(&#39;身份证应为18位,请正确填写&#39;);
 }else if(qq.length<=5 &&qq.length>=13){
  judge = false ;
  alert(&#39;请正确输入qq号码&#39;);
 }else if(phone.length<3&&qq.length>12){
  judge = false ;
  alert(&#39;请正确输入电话&#39;);
 }else if(!reg_email.test(email)){
  judge = false ;
  alert(&#39;邮箱格式不正确&#39;);
 }else if(!reg_name.test(username)){
  judge = false ;
  alert(&#39;用户名格式不正确&#39;);
 }else if(!reg_chinese.test(name)){
  judge = false ;
  alert(&#39;姓名格式不正确&#39;);
 }else if((!reg_pass.test(password))||password.length<6){
  judge = false ;
  alert(&#39;密码格式不正确&#39;);
 }
  
 return judge ;
}
//----判断输入框的信息是否符合要求-----
 
//----新增信息的插入方法-----
function insertInfo(){
 //根据id获取表单信息
 var arr = new Array();
 arr[0] = document.getElementById(&#39;username&#39;).value;
 arr[1] = document.getElementById(&#39;password&#39;).value; 
 arr[2] = document.getElementById(&#39;name&#39;).value;
 arr[3] = document.getElementById(&#39;email&#39;).value;
 arr[4] = document.getElementById(&#39;phone&#39;).value;
 arr[5] = document.getElementById(&#39;qq&#39;).value;
 arr[6] = document.getElementById(&#39;uid&#39;).value;
 arr[7] ="<a style=&#39;text-align:center;color:blue;cursor:pointer;&#39; onclick=&#39;updateRow(this);&#39;>修改</a> <a style=&#39;text-align:center;color:blue;cursor:pointer;&#39; onclick=&#39;delRow(this);&#39;>删除</a>";
 var x = document.getElementById(&#39;table&#39;).insertRow(1); //获取第一行对象
  
 for(var i=0;i<arr.length;i++){
  x.insertCell(i).innerHTML = arr[i] ; //用循环把每个数据插入第一行的每一列
 }
  
}
//----新增信息的插入方法-----
 
//----新增信息-----
function addInfo(){
  
 if(judge()==true){
  alert(&#39;添加成功&#39;);
  insertInfo(); //执行插入
  hideAddInput(); //隐藏添加信息框
   
 }else{
  alert(&#39;添加失败&#39;);
 }
}
//----新增信息-----
 
 
//----根据行号修改信息-----
function updateRow(r){
 row = getRow(r); //把该行号赋值给全局变量
 showAddInput(); //显示修改表单
 //提交按钮替换
 document.getElementById(&#39;btn_add&#39;).style="display:none" ;
 document.getElementById(&#39;btn_update&#39;).style="display:block-inline" ;
 insertInputFromQuery(queryInfoByRow(row));
  
}
//----根据行号修改信息-----
 
 
//----根据行号查信息----
function queryInfoByRow(r){
  
 var arr = new Array();
 for(var m=0 ; m<7;m++){
  arr[m] = document.getElementById(&#39;table&#39;).rows[row].cells[m].innerText;
 }
 return arr ; //返回该行数据
  
}
//----根据行号查信息----
 
//----把查询到的信息放入修改的表单里----
function insertInputFromQuery(arr){
 document.getElementById(&#39;username&#39;).value = arr[0];
 document.getElementById(&#39;password&#39;).value = arr[1];
 document.getElementById(&#39;name&#39;).value = arr[2];
 document.getElementById(&#39;email&#39;).value = arr[3];
 document.getElementById(&#39;phone&#39;).value = arr[4];
 document.getElementById(&#39;qq&#39;).value = arr[5];
 document.getElementById(&#39;uid&#39;).value = arr[6];
  
}
//----把查询到的信息放入修改的表单里----
 
 
function updateInfo(){
 if(judge()==true){
  alert(&#39;修改成功&#39;);
  document.getElementById(&#39;table&#39;).deleteRow(row);//删除原来那行  
  insertInfo(); //插入修改后的值
  hideAddInput(); //隐藏添加模块
 }else{
  alert(&#39;修改失败&#39;);
  hideAddInput();
 }
}

The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope that everyone will support the PHP Chinese website.

For more articles related to adding, deleting, and modifying HTML tables with JS, please pay attention to the PHP Chinese website!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!