ajax前后端配合,马克一下,方便查询
jquery开头和取得相应id的值
1 $(document).ready(function(){ 2 3 $("#sj").click(function(){ 4 var k=$("#pro").val(); 5 var st=$("#pro").val(); 6 alert(k); 7 }); 8 9 });
添加ajax和后台
1 $.ajax({ 2 url:"{:U('Index/pajax')}", 3 data:{ 4 "sid":k, 5 "st":st 6 }, 7 type:"post", 8 success:function(result){ 9 $("#div1").html(result); 10 11 alert(result); 12 } 13 });
后台
1 public function pajax(){ 2 if(IS_POST){ 3 4 show_bug($_POST,"后台数据"); 5 } 6 }
贴上完整的代码,后端表两个一个是目录 ,一个是内容,用的开源的qw后台。
前台js,product是需要插入的内容
1 <script> 2 $(document).ready(function(){ 3 4 $("#sj").click(function(){ 5 var k=$("#pro").val(); 6 var st=$("#custom_con").val(); 7 8 9 $.ajax({ 10 url:"{:U('Index/pajax')}", 11 data:{ 12 "sid":k, 13 "st":st 14 }, 15 type:"post", 16 success:function(result){ 17 $("#products").empty(); 18 $("#page").empty(); 19 $("#products").html(result); 20 21 // alert(result); 22 } 23 }); 24 25 26 27 }); 28 29 });
后台方法
1 public function pajax(){ 2 if(IS_POST){ 3 $cat=M('category')->where("pid=36")->select(); 4 if(I('sid')===0){ 5 foreach ($cat as $key=>$value){ 6 if($key==0){ 7 $condition="sid=".$value['id']; 8 }else{ 9 $condition=$condition." OR sid=".$value['id']; 10 } 11 } 12 } 13 else{ 14 $condition="sid=".$_POST['sid']; 15 } 16 if(I('st')){ 17 $condition.=" and title like '%".I('st')."%'"; 18 } 19 $products=M('article')->where($condition)->select(); 20 $this->assign("products",$products); 21 $this->display('pajax'); 22 } 23 }
后台的展示模版
1 <ul> 2 <volist name="products" id="vo"> 3 <li class="program"> 4 <div class="program_top"> 5 <img src="{$vo.thumbnail}" alt=""/> 6 <a href=""> 7 <div class="program_model"> 8 <img src="__PUBLIC__/home/images/search1.png" alt=""/> 9 <p>查看详情</p> 10 </div> 11 </a> 12 </div> 13 <span>{$vo.title}</span> 14 </li> 15 </volist> 16 </ul>