AJAX删除
全选单选+ajax方法: toggle+porp(一般用在全选单选)
prop获取属性 也可以使用attr()如需检索 HTML 属性,请使用 attr() 方法代替
思想:为了提交用的体验,我们使用异步数据操作 实现页面无刷新操作,。 ajax有很多种使用写法,第一种【在html页面写$ajax方法 url:写入后台某一个控制器的某一个方法,data:用post/get提交方式,】
ajax第一种书写方式:
$.ajax({
url:'http://xxx/public/index.php/back/topic/ajaxList',
type:'post',
dataType:'json',
data: {apage:page},
success:function(data){ // success成功后返回什么、、、、data是回调函数
//console.log(data)
},
error:function(){ //错误返回的数据
},
async:true // 同步异步true、false
$("#result").html(data.page);
}
});
}
ajax第二种方法:
删除
$('.del1').click(function(){
var id = $(this).attr("rowid");
$.post("{:url('del')}",{id:id},function(msg){
if(msg.num==1){
alert(msg.con);
$(this).parents("tr").remove();
}else{
alert(msg.con);
}
})
})
全选/单选 $(".quan").toggle(function(){
$("input[type=checkbox]").prop("checked",true);
},function(){
$("input[type=checkbox]").prop("checked",false);
});
批量删除
$('.del').click(function(){
var arr = [];
$("input[type=checkbox]:checked").each(function(k,v){
arr.push($(this).val());
})
$.post("{:url('pidel')}",{arr:arr},function(msg){
console.log(msg);
if (msg.num == 1) {
alert(msg.con);
$("input[type=checkbox]:checked").each(function(k,v){
$(this).parents("tr").remove();
});
}else{
alert(msg.con);
}
})
});
Controller控制器方法写:
// ajax批量删除
public function pidel(){
$id = input("post.arr/a");
if (empty($id)) {
$data['num'] = 0;
$data['con'] = "操作错误,未选中数据";
}
$res = db('goods')->delete($id);
if ($res) {
$data['num'] = 1;
$data['con'] = "删除成功";
}else{
$data['num'] = 0;
$data['con'] = "删除失败";
}
return json($data);
}