Home >Web Front-end >JS Tutorial >Add, delete, and modify HTML tables using JS

Add, delete, and modify HTML tables using JS

高洛峰
高洛峰Original
2017-01-07 10:15:021973browse

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>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