jquery ajax全选以及修改状态
全选并处理字符串
function check_val(){ obj = $(".table .data-id") //表格处class 添加class="table";例如:<table class="table">
check = []; //全选字段处添加id="qx" 对应的全选格处中添加class="data-id" 例如<tr><th id="qx">全选</th></tr> for(k in obj){ // <tr><td><input type="checkbox" class="data-id"></td></tr> if(obj[k].checked){ check.push(obj[k].value); } } return check; } var check = true; $("#qx").click(function(){ if(check==true) { checkAll('checkall'); check = false; } else { clearAll('checkall'); check = true; } }); function checkAll(name) { var el = document.getElementsByTagName('input'); var len = el.length; for(var i=0; i<len; i++){ if((el[i].type=="checkbox") && (el[i].name==name)){ el[i].checked = true; } } } function clearAll(name) { var el = document.getElementsByTagName('input'); var len = el.length; for(var i=0; i<len; i++){ if((el[i].type=="checkbox") && (el[i].name==name)){ el[i].checked = false; } } } } } 是否删除 $(".redel1").click(function(){ if (!confirm("确认要删除吗?")) { window.event.returnValue = false; }else { var id = check_val(); $.ajax({ url: "{:U('Index/ajaxRedel1')}", //本行中ajaxRedel1 要与控制器函数名一致 type: "post", dataType: "json", data: {id: id,table:'email_p'}, //table 后为数据库表名 success: function (data) { if (data.id == 1) { alert("删除成功"); window.location.reload() } } }) } });
后台控制器
public function ajaxRedel1(){ //可修改函数名, 本例中为修改$table表中的stasuss参数,依据自身需求修改 /* $table = I("post.table"); //删除 $where['id'] = array("in",I("post.id")); $res = M("{$table}")->where($where)->delete(); if( $res ){ $this->ajaxReturn(array('id' => 1 )); }*/ $table = I("post.table"); //修改statuss参数
$data['statuss'] = '-1'; $where['pid'] = array("in",I("post.id")); $res = M("{$table}")->where($where)->save($data); if( $res ){ $this->ajaxReturn(array('id' => 1 )); } }
浙公网安备 33010602011771号