前端
view
admin/manager/index.html
<td>
<%if(list[i].status == 1){%>
<img src="/public/admin/images/yes.gif" onclick="app.changeStatus(this,'Admin','status','<%=list[i]._id%>')" alt="">
<%}else{%>
<img src="/public/admin/images/no.gif" onclick="app.changeStatus(this,'Admin','status','<%=list[i]._id%>')" alt="">
<%}%>
</td>

前端公共base.js
app/public/admin/js/base.js
$(function () {
app.init()
})
var app = {
init: function () {
this.toggleAside();
},
toggleAside: function () {
$('.aside h4').click(function () {
$(this).siblings('ul').slideToggle();
})
},
//改变状态
changeStatus:function(el,model,attr,id){
$.get('/admin/changeStatus',{model:model,attr:attr,id:id},function(data){
if(data.success){
if(el.src.indexOf('yes') != -1){
el.src = '/public/admin/images/no.gif'
}else{
el.src = '/public/admin/images/yes.gif'
}
}
})
}
}
后台
router.js
router.get('/admin/changeStatus', controller.admin.base.changeStatus);
controler
app/controller/admin/base.js
async changeStatus(){
var model = this.ctx.request.query.model;
var attr = this.ctx.request.query.attr;
var id = this.ctx.request.query.id;
var result = await this.ctx.model[model].find({"_id":id});
if(result.length >0){
if(result[0][attr] == 1){
var json = {
[attr]:0
}
}else{
var json = {
[attr]:1
}
}
var updateResult = await this.ctx.model[model].updateOne({"_id":id},json);
if(updateResult){
this.ctx.body = {"message":"更新成功","success":true}
}else{
this.ctx.body = {"message":"更新失败","success":false}
}
}else{
this.ctx.body = {"message":"更新失败,参数错误","success":false}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。