1 Star 0 Fork 0

ayu/测试仓库2

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
01-5图书管理模板引擎.html 3.80 KB
Copy Edit Raw Blame History
ayu authored 2020-08-12 09:48 . 这里全是ajax的练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<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 type='text' id='tpl'>
{{each data}}
<tr>
<th>{{$value.id}}</th>
<th>{{$value.bookname}}</th>
<th>{{$value.author}}</th>
<th>{{$value.publisher}}</th>
<th><a href="javascript:;" class="del" data-id='{{$value.id}}'>删除</a></th>
</tr>
{{/each}}
</script>
<script>
$(function() {
getbooklist();
function getbooklist() {
$.get('http://www.liulongbin.top:3006/api/getbooks', function(res) {
if (res.status !== 200) return false;
var htmlstr = template('tpl', res);
$('#tb').html(htmlstr);
})
}
//动态创建的元素,直接给del绑定事件会绑定不上. 所以要用到事件委托
$('tbody').on('click', '.del', function() {
var id = $(this).attr('data-id');
console.log(id);
$.get('http://www.liulongbin.top:3006/api/delbook', {
id: id
}, function(res) {
console.log(res.status);
if (res.status !== 200) return alert('delete false');
getbooklist();
})
})
$('#btnAdd').on('click', function() {
var bookname = $('#iptBookname').val().trim();
var iptAuthor = $('#iptAuthor').val().trim();
var iptPublisher = $('#iptPublisher').val().trim();
if (bookname.length <= 0 || iptAuthor <= 0 || iptPublisher <= 0) return alert('添加失败!');
getbooklist();
$.post('http://www.liulongbin.top:3006/api/addbook', {
bookname: bookname,
author: iptAuthor,
publisher: iptPublisher
}, function(res) {
if (res.status !== 201) return alert('添加失败');
getbooklist();
})
})
})
</script>
<script src="./lib/template-web.js"></script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ayu999/project_03.git
git@gitee.com:ayu999/project_03.git
ayu999
project_03
测试仓库2
master

Search