发布于 

图书管理

html部分:

只是一段代码没注释也不是很多

enter编码13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<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" />
</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标签 -->
</tbody>
</table>
</body>

js部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

<script src="./lib/jquery.js"></script>
<script>
renderBook();
$('#btnAdd').on('click', function() {
var val1 = $('#iptBookname').val().trim();
var val2 = $('#iptAuthor').val().trim();
var val3 = $('#iptPublisher').val().trim();

if (val1 === '' || val2 === '' || val3 === '') {
alert('请输入完整的数据');
return;
}

var kids = '添加书的接口addbooks';
$.post(kids, {
bookname: val1,
author: val2,
publisher: val3
}, function(res) {
if (res.status === 201) {
renderBook();
$('#bookname').val();
$('#author').val();
$('#publisher').val();
}
})
});

function renderBook() {
var kid = '获取书的接口getbooks';
$.get(kid, function(res) {

if (res.status !== 200) {
return;
};
var data = res.data;
console.log(data)
var str = '';
$.each(data, function(k, r) {
str +=
' <tr>\
<td>' + r.id + '</td>\
<td>' + r.bookname +
'</td>\
<td>' + r.author + '</td>\
<td>' + r.publisher +
'</td>\
<td><a href="javascript:;">删除 </a><td>\
</tr>; '
});
$('#tb').html(str);
});
}
</script>

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。