zk 加载 html 标签,index-zk.html

Document

添加新图书

书名
作者
出版社

添加

Id书名作者出版社操作

$(function () {

getBooks()

function getBooks() {

// 每次渲染要进行清除

$('#tb').empty()

//获取图书、

$.ajax({

type: 'get',

url: 'http://www.liulongbin.top:3006/api/getbooks',

success: function (res) {

console.log(res);

// 对图书data进行遍历

res.data.forEach(function (item) {

// 使用模板字符串进行添加元素

var tr = `

${item.id}${item.bookname}${item.author}${item.publisher} 删除

`

// 将新建元素添加到tbody中

$('#tb').append(tr)

})

}

})

}

// 添加图书

$('#btnAdd').click(function () {

var bookname = $('#iptBookname').val().trim()

var author = $('#iptAuthor').val().trim()

var 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('添加图书失败!')

}

$('#iptBookname').val('')

$('#iptAuthor').val('')

$('#iptPublisher').val('')

// 重新渲染

getBooks()

}

})

})

$('#iptPublisher').keyup(function (e) {

if (e.keyCode == 13) {

$('.addbook').click()

}

})

// 删除图书

// 给删除按钮添加事件委托

$('#tb').on('click', '.del', function () {

// 获取当前对象的 id

var ID = $(this).attr('data-id')

// 询问用户是否确定删除

var result = confirm('确认删除吗')

// 如果确定直接删除对象

if (result) {

$.ajax({

type: 'get',

url: 'http://liulongbin.top:3006/api/delbook',

data: { id: ID },

success: function (res) {

console.log(res);

if (res.status != 200) {

return alert('删除失败!')

}

// 删除之后重新渲染页面

getBooks()

}

})

}

})

})

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值