关于JQ的AJAX

  1. 本文实例讲述了ThinkPHP实现ajax仿官网搜索功能的方法。分享给大家供大家参考。
  2. 具体实现方法如下:
  3. 后台代码:
  4. 代码如下:
  5. //搜索,如果在1不在0  
  6.  function search(){  
  7.      $keyword = $_POST['search'];  
  8.      $Goods=M('goods');  
  9.    //这里我做的一个模糊查询到名字或者对应的id,主要目的因为我这个系统是  
  10.    //商城系统里面用到直接看产品ID  
  11.      $map['goods_id|goods_name']  = array('like','%'.$keyword.'%');  
  12.      // 把查询条件传入查询方法  
  13.      if($goods=$Goods->where($map)->select())  
  14.       {  
  15.                $this->ajaxReturn($goods,'查询成功!',1);  
  16.       }else{  
  17.                $this->ajaxReturn($data,"查询失败,数据不存在!",0);  
  18.   }  
  19.  }
  20. 前端代码:
  21. 代码如下:
  22. $(document).ready(function(){  
  23.     $(".show_message").hide();  
  24.     var $search=$('#search_box');  
  25.     $("#submit_from").click(function(){  
  26.      if($("#search_box").attr("value")=='')  
  27.      {  
  28.          //alert('请输入文字!');  
  29.          $(".show_message").html('错误提示:搜索框文本不能为空!');  
  30.          $(".show_message").fadeIn(1000);  
  31.          $(".show_message").fadeOut(1000);  
  32.          $search.focus();  
  33.          //return false;  
  34.      }else{  
  35.          //开始ajax执行数据  
  36.          $.ajax({  
  37.              type: "POST",  
  38.              url:"/index.php/Goods/search",  
  39.              data:{  
  40.                  search:$search.val()  
  41. //data: {hah:aaa,hah2:bbb}
  42.              },  
  43.              dataType: "json",  
  44.              success: function (data) {  
  45.      if (data.status == 1) {  
  46.              //alert(data.info);  
  47.              var html='';  
  48.                      $.each(data.data,function(no,items){      
  49.                      html+='';  
  50.                      });  
  51.                      html+="  
  52.  '+items.goods_id+' '+items.goods_name+' '+items.add_time+' '+items.brand+' '+items.price+'";      
  53.                       $(".goods-list").html(' ').html(html);  
  54.                     // alert(html);  
  55.      }  
  56.      else if (data.status == 0) {  
  57.          $(".show_message").show();  
  58.          $(".show_message").html(data.info);  
  59.                      $(".show_message").fadeOut(3000);  
  60.      //    alert(data.info);  
  61.            return false;  
  62.          }  
  63.        }  
  64.           });  
  65.      }  
  66.    });  
  67.  });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值