我的账户
猩码学苑

专注C++开发菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

雷盟-前端-20230106

[复制链接]
奔黄昏而去学员认证 发表于 2023-1-31 19:33:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Document</title>
  8.     <script src="./axios.js"></script>
  9.     <style>
  10.         html,
  11.         body {
  12.             width: 800px;
  13.             height: 500px;
  14.             margin: 20px auto;
  15.         }
  16.         table,
  17.         td,
  18.         th,
  19.         tr {
  20.             border: 1px solid #ccc;
  21.             padding: 2px;
  22.         }
  23.         table {
  24.             width: 800px;
  25.             border-collapse: collapse;
  26.         }
  27.         #big {
  28.             width: 800px;

  29.             position: absolute;
  30.         }
  31.         #sou {
  32.             height: 100px;
  33.         }
  34.         #ipt {
  35.             line-height: 20px;
  36.             text-align: center;
  37.             margin-left: 10px;
  38.         }

  39.         #add {
  40.             padding-top: 10px;
  41.             padding-left: 50px;
  42.             margin: 50px auto;
  43.             background-color: aquamarine;
  44.             height: 250px;
  45.             width: 300px;
  46.         }

  47.         #tan {
  48.             width: 200px;
  49.             height: 80px;
  50.             border: 1px solid #666;
  51.             text-align: center;
  52.             background-color: rgb(168, 219, 219);
  53.             padding-bottom: 15px;
  54.             position: relative;
  55.             top: -346px;
  56.             left: 290px;
  57.             display: none;
  58.         }
  59.         #xiu {
  60.             width: 300px;
  61.             height: 100px;
  62.             border: 1px solid #666;
  63.             text-align: center;
  64.             background-color: rgb(168, 219, 219);
  65.             padding-bottom: 15px;
  66.             position: relative;
  67.             top: -330px;
  68.             left: 245px;
  69.             display: none;
  70.         }
  71.         #tab {
  72.             margin-top: 20px;
  73.         }
  74.     </style>
  75. </head>
  76. <body>
  77.     <div id="big">
  78.         <div id="add">
  79.             <p>姓名 <input type="text" id="name"></p>
  80.             <p>年龄 <input type="text" id="age"></p>
  81.             <p>性别 <input type="text" id="sex"></p>
  82.             <p>电话 <input type="text" id="tel"></p>
  83.             <p>密码 <input type="password" id="pas"></p>
  84.             <p><button id="btn4">添加</button></p>
  85.         </div>
  86.         <div id="sou">
  87.             ID <input type="text" id="ipt" placeholder="请输入查询">
  88.             <button id="btn">搜索</button>
  89.             <table>
  90.                 <thead>
  91.                     <tr>
  92.                         <th>id</th>
  93.                         <th>姓名</th>
  94.                         <th>年龄</th>
  95.                         <th>性别</th>
  96.                         <th>电话</th>
  97.                         <th>操作</th>
  98.                     </tr>
  99.                 </thead>
  100.                 <tbody>
  101.                     <tr>
  102.                         <td> </td>
  103.                         <td> </td>
  104.                         <td> </td>
  105.                         <td> </td>
  106.                         <td> </td>
  107.                         <td> </td>
  108.                     </tr>
  109.                 </tbody>
  110.             </table>
  111.         </div>

  112.         <div id="tab">
  113.             <table>
  114.                 <thead>
  115.                     <tr>
  116.                         <th>id</th>
  117.                         <th>姓名</th>
  118.                         <th>年龄</th>
  119.                         <th>性别</th>
  120.                         <th>电话</th>
  121.                         <th>操作</th>
  122.                     </tr>
  123.                 </thead>
  124.                 <tbody>
  125.                 </tbody>
  126.             </table>
  127.         </div>

  128.         <div id="tan">
  129.             <div id="del">
  130.                 <p>请确认是否删除</p>
  131.                 <button id="yes">确认</button>
  132.                 <button id="no">取消</button>
  133.             </div>
  134.         </div>
  135.         <div id="xiu">
  136.             <p> 姓名:<input type="text" id="newName"></p>
  137.             <p><button id="btn3">确认修改</button></p>
  138.         </div>
  139.         <div id="page">
  140.         </div>
  141.     </div>
  142.     <script>
  143.         var ps = document.querySelector('#add').querySelectorAll('input');
  144.         var big = document.querySelector('#big');
  145.         var ipt = document.querySelector('#ipt');
  146.         var btn = document.querySelector('#btn');
  147.         var sou = document.querySelector('#sou');
  148.         var tab = document.querySelector('#tab');
  149.         var btn1 = document.querySelector('.btn1');
  150.         var btn2 = document.querySelector('.btn2');
  151.         var btn11 = document.querySelector('.btn11');
  152.         var btn22 = document.querySelector('.btn22');
  153.         var tan = document.querySelector('#tan');
  154.         var del = document.querySelector('#del');
  155.         var yes = document.querySelector('#yes');
  156.         var no = document.querySelector('#no');
  157.         var xiu = document.querySelector('#xiu');
  158.         var newName = document.querySelector('#newName');
  159.         var btn3 = document.querySelector('#btn3');
  160.         var btn4 = document.querySelector('#btn4');
  161.         // 给tab渲染list数据
  162.         function renderList() {
  163.             // 发送ajax--接收响应的数据
  164.             axios.get('http://162.14.107.109/list').then(res => {
  165.                 // 成功的回调
  166.                 console.log(res);
  167.                 // console.log(res.data.data);
  168.                 var stuList = res.data.data.data;
  169.                 var html = '';
  170.                 //  遍历数组
  171.                 stuList.forEach((v) => {
  172.                     // console.log(v);
  173.                     html += `  <tr>
  174.                 <td>${v.id}</td>
  175.                 <td>${v.name}</td>
  176.                 <td>${v.age}</td>
  177.                 <td>${v.sex}</td>
  178.                 <td>${v.tel}</td>
  179.                 <td><button class='btn11'>删除</button><button class='btn22'>修改</button></td>
  180.             </tr>`
  181.                 });
  182.                 tab.lastElementChild.lastElementChild.innerHTML = html;
  183.             }).catch(err => {
  184.                 // 失败的回调
  185.             });
  186.         };
  187.         renderList();
  188.         // 搜索按钮
  189.         btn.addEventListener('click', function () {
  190.             // 获取ipt的value
  191.             let iptVal = ipt.value;
  192.             // axios方法
  193.             axios.get('http://162.14.107.109/show?id=' + ipt.value).then(res => {
  194.                 // 成功的回调
  195.                 console.log(res);
  196.                 var stuList2 = res.data.data;
  197.                 sou.lastElementChild.lastElementChild.innerHTML = `
  198.         <tr>
  199.         <td>${stuList2.id}</td>
  200.         <td>${stuList2.name}</td>
  201.         <td>${stuList2.age}</td>
  202.         <td>${stuList2.sex}</td>
  203.         <td>${stuList2.tel}</td>
  204.         <td><button class='btn11'>删除</button><button class='btn22'>修改</button></td>
  205. `;
  206.             }).catch(err => {
  207.                 // 失败的回调
  208.             });
  209.             iptVal.value = "";
  210.         });
  211.         // 搜索框里的按钮事件
  212.         sou.addEventListener('click', function (e) {
  213.             // 给删除添加点击事件
  214.             if (e.target.className == 'btn11') {
  215.                 // 跳出弹窗
  216.                 tan.style.display = "block";
  217.                 // 同时修改按钮禁用
  218.                 xiu.style.display = "none";
  219.             }
  220.             // 给修改添加点击事件
  221.             if (e.target.className == 'btn22') {
  222.                 // 点击修改按钮显示修改弹窗
  223.                 xiu.style.display = "block";
  224.                 tan.style.display = "none";
  225.                 axios.get(`http://162.14.107.109/update?id=${ipt.value}&name=${newName.value}`).then(res => {
  226.                     // 成功的回调
  227.                     console.log(res);
  228.                     console.log(res.data.data);
  229.                     var stuList5 = res.data.data.data;
  230.                     newName.value = stuList5.name;
  231.                 }).catch(err => {
  232.                     // 失败的回调
  233.                 });
  234.             }
  235.         });
  236.         // 给删除弹窗里的确认按钮添加点击事件
  237.         yes.addEventListener('click', function () {
  238.             tan.style.display = "none";
  239.             // 删除
  240.         });
  241.         // 给删除弹窗里的取消按钮添加点击事件
  242.         no.addEventListener('click', function () {
  243.             tan.style.display = "none";
  244.         });
  245.         // 点击确认修改按钮把修改的数据渲染到tr里面
  246.         btn3.addEventListener('click', function () {
  247.             xiu.style.display = "none";
  248.             axios.get(`http://162.14.107.109/update?id=${ipt.value}&name=${newName.value}`).then(res => {
  249.                 // 成功的回调
  250.                 console.log(res);
  251.                 var stuList5 = res.data.data.data;
  252.                 stuList5.name = newName.value;
  253.             }).catch(err => {
  254.                 // 失败的回调
  255.             });
  256.         });
  257.         // 新增提交按钮
  258.         btn4.addEventListener('click', function () {
  259.             axios.get(`http://162.14.107.109/create?name=${ps[0].value}&age=${ps[1].value}&sex=${ps[2].value}&password=${ps[4].value}&tel=${ps[3].value}`).then(res => {
  260.                 // 成功的回调
  261.                 console.log(res);
  262.                 var stuList5 = res.data.data.data;
  263.                 res.data.data.removeChild(stuList5);
  264.                 let stuList4 = JSON.parse(xhr.responseText).data;
  265.                 ps[0].value = stuList4.name;
  266.                 ps[1].value = stuList4.age;
  267.                 ps[2].value = stuList4.sex;
  268.                 ps[3].value = stuList4.passwords;
  269.                 ps[4].value = stuList4.tel;
  270.             }).catch(err => {
  271.                 // 失败的回调
  272.             });
  273.         }
  274.         );
  275.     </script>
  276. </body>
  277. </html>
复制代码


回复

使用道具 举报

关注0

粉丝0

帖子48

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注C++菁英教育

客服电话:18009298968

客服时间:9:00-21:00

猩码学苑 - 专注C++开发菁英教育!( 陕ICP备2025058934号-1 )

版权所有 © 陕西菁英数字科技有限公司 2023-2026