操作:
<input type="button" name="" class="btn" id="clearBtn" value="清空">
前端代码:
$('#clearBtn').click(function(){ $.ajax({ type:'post',url:'index.PHP?act=groupbuy&op=slider_clear',dataType:'json',success:function(result){ if(result.result){ alert('清空成功'); location.reload(); } } }); });
后端代码:
/** *幻灯片清除 */ publicfunctionslider_clearOp(){ $model_setting=Model('setting'); $update=array(); $update['live_pic1']=''; $update['live_link1']=''; $update['live_pic2']=''; $update['live_link2']=''; $update['live_pic3']=''; $update['live_link3']=''; $update['live_pic4']=''; $update['live_link4']=''; $res=$model_setting->updateSetting($update); if($res){ dkcache('setting'); $this->log('清空幻灯片设置',1); echojson_encode(array('result'=>'true')); }else{ echojson_encode(array('result'=>'false')); } exit; }
ajax返回来的数据有的时候是json字符串需要将其转化为json对象
alldata = eval('(' + data + ')');
选择框的ajax
$(function(){ selectajax(); $('#inform_type').on('change',(function(){ selectajax(); })); functionselectajax(){ id=$('#inform_type').val(); $.ajax({ type:'post',url:"index.PHP?act=member_inform&op=selectAjax",data:{id:id},success:function(data){ varlen=(data.length); $('#inform_subject').empty(); for(vari=0;i<len;i++){ $('#inform_subject').append("<optionvalue=\""+data[i]["inform_subject_id"]+"\">"+data[i]["inform_subject_content"]+"</option>"); } } }); } }
后端代码:
<?PHP publicfunctionselectAjaxOp(){ $id=$_POST['id']; $inform_subject_model=Model('inform_subject'); $inform_subject=$inform_subject_model->getSubjectList(array('inform_type_id'=>$id),'inform_subject_id,inform_subject_content'); echojson_encode($inform_subject); }原文链接:https://www.f2er.com/ajax/162376.html