状态改变思路:
1:给以个class类和一个点击事件
2:获取当前的id和值
3:判断值与数据库里的字段值是否一样
4:传值 后台处理
5:替换
代码:
表中的一行,就是现实状态的哪一行(class="zt")(tp5做的)
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<th><input type="checkbox"></th>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>封面</th>
<th>状态</th>
<th>出售时间</th>
<th>操作</th>
</tr>
{volist name="arr" id="v"}
<tr id="{$v.bid}">
<td><input type="checkbox" name="box" value="{$v.bid}"></td>
<td>{$v.bid}</td>
<td fd="name"><span>{$v.name}</span></td>
<td fd="author"><span>{$v.author}</span></td>
<td><img src="__STATIC__/{$v.photo}" style="width: 100px;height: 70px;"></td>
<td><p class="zt">{$v.state}</p></td>
<td>{$v.time}</td>
<td><a href="">删除</a></td>
</tr>
{/volist}
</table>
<script src="__STATIC__/jquery.js"></script>
<!--状态改变-->
<script>
$(document).on('click','.zt',function(){
var id=$(this).parents('tr').attr('id');
var txt=$(this).text();
var obj=$(this);
if(txt=='启用') {
p='禁用';
}else{
p='启用'
}
$.get("{:url('index/zt')}", { p:p ,id:id},
function(data){
if(data==1)
{
obj.html('<p>'+p+'</p>');
}
});
});
</script>
控制器
//状态改变
public function zt()
{
$id=input('get.id');
$arr['state']=input('get.p');
$user=new Books();
$res=$user->upd($id,$arr);
if($res)
{
return 1;
}
}
模块
//状态改变
public function upd($id,$arr)
{
return $this->where('bid',$id)->update($arr);
}