原生JavaScrip ajax图书列表的增删

渲染页面

删除图书(根据id名)

添加图书

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./lib/bootstrap.css" />
    <script src="./lib/jquery.js"></script>
  </head>
  <body style="padding: 15px">
    <!-- 添加图书的Panel面板 -->
    <div class="panel panel-primary">
      <div class="panel-heading">
        <h3 class="panel-title">添加新图书</h3>
      </div>
      <div class="panel-body form-inline">
        <div class="input-group">
          <div class="input-group-addon">书名</div>
          <input
            type="text"
            class="form-control"
            id="iptBookname"
            placeholder="请输入书名"
          />
        </div>

        <div class="input-group">
          <div class="input-group-addon">作者</div>
          <input
            type="text"
            class="form-control"
            id="iptAuthor"
            placeholder="请输入作者"
          />
        </div>

        <div class="input-group">
          <div class="input-group-addon">出版社</div>
          <input
            type="text"
            class="form-control"
            id="iptPublisher"
            placeholder="请输入出版社"
          />
        </div>

        <button id="btnAdd" class="btn btn-primary">添加</button>
      </div>
    </div>

    <!-- 图书的表格 -->
    <table class="table table-bordered table-hover">
      <thead>
        <tr>
          <th>Id</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody id="tb"></tbody>
    </table>
    <script>
      $(function () {
        //1.渲染页面
        getbooksList();
        //实现删除(根据id)
        $("tbody").on("click", "a", function () {
          //获取id
          let bookId = $(this).attr("data-id");
          //发送ajax删除数据
          $.ajax({
            type: "get",
            url: "http://www.liulongbin.top:3006/api/delbook",
            data: {
              id: bookId,
            },
            success: function (res) {
              if (res.status != 200) return alert(res.msg);
              getbooksList();
            },
          });
        });

        //实现添加
        $("#btnAdd").on("click", function () {
          //获取输入框内容
          let bookname = $("#iptBookname").val().trim();
          let author = $("#iptAuthor").val().trim();
          let publisher = $("#iptPublisher").val().trim();
          if (
            bookname.length == 0 ||
            author.length == 0 ||
            publisher.length == 0
          ) {
            return alert("请输入内容,内容不能为空");
          }
          //把数据发送给服务器
          $.ajax({
            type: "post",
            url: "http://www.liulongbin.top:3006/api/addbook",
            data: {
              bookname: bookname,
              author: author,
              publisher: publisher,
            },
            success: function (res) {
              //判断数据是否添加成功
              if (res.status != 201) return alert(res.msg);
              getbooksList();
            },
          });
          $("#iptBookname").val("");
          $("#iptAuthor").val("");
          $("#iptPublisher").val("");
        });
        function getbooksList() {
          //获取图书列表并展示
          $.get("http://www.liulongbin.top:3006/api/getbooks", function (res) {
            //判断获取数据是否成功
            if (res.status != 200) return alert(res.msg);
            //循环遍历数组
            //声明空的数组用于存放tr
            let rows = [];
            res.data.forEach(function (item) {
              //生成tr
              // console.log(item);
              let tr = `<tr>
                    <td>${item.id}</td>
                    <td>${item.bookname}</td>
                    <td>${item.author}</td>
                    <td>${item.publisher}</td>
                    <td><a href="javascript:;" class="del" data-id=${item.id}>删除</a></td>
                  </tr>`;
              //把tr添加到rows里面
              rows.push(tr);
            });
            //将tr渲染到tbody中
            $("tbody").empty().append(rows.join());
          });
        }
      });
    </script>
  </body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

♡ 小宸轩的前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值