Home  >  Article  >  php教程  >  ajax仿官网搜索功能

ajax仿官网搜索功能

WBOY
WBOYOriginal
2016-06-07 11:44:331221browse

ajax仿官网搜索功能献丑了送给那些被为ajax折磨的同学
后台实现代码//搜索苍老师,如果苍老师在1不再0<br> function search(){<br>     $keyword = $_POST['search'];<br>     $Goods=M('goods');<br>   //这里我做的一个模糊查询到名字或者对应的id,主要目的因为我这个系统是<br>   //商城系统里面用到直接看产品ID<br>     $map['goods_id|goods_name']  = array('like','%'.$keyword.'%');<br>     // 把查询条件传入查询方法<br>     if($goods=$Goods->where($map)->select())<br>     {<br>     $this->ajaxReturn($goods,'查询成功!',1);<br> <br>      }else{<br>     $this->ajaxReturn($data,"查询失败,数据不存在!",0);<br>      }<br>     }前端代码<script><br /> $(document).ready(function(){<br /> $(".show_message").hide();<br /> var $search=$('#search_box');<br /> $("#submit_from").click(function(){<br /> if($("#search_box").attr("value")=='')<br /> {<br /> //alert('请输入文字!');<br /> $(".show_message").html('错误提示:搜索框文本不能为空!');<br /> $(".show_message").fadeIn(1000);<br /> $(".show_message").fadeOut(1000);<br /> $search.focus();<br /> //return false;<br /> }else{<br /> //开始ajax执行数据<br /> $.ajax({<br /> type: "POST",<br /> url:"/index.php/Goods/search",<br /> data:{<br /> search:$search.val()<br /> },<br /> dataType: "json",<br /> success: function (data) {<br /> if (data.status == 1) {<br /> //alert(data.info);<br /> var html='<table class="striped" id="senfe"><thead class="striped-top"><tr><th>商品编号<th class="right">商品名字<th class="right">添加日期<th class="right">商品品牌<th class="right">商品价格<tbody>';<br /> /*<th class="right">编辑<th class="right">删除*/<br /> $.each(data.data,function(no,items){ <br /> html+='<tr><td>'+items.goods_id+'<td class="right"><a href="/Goods/show/id/'+items.goods_id+'">'+items.goods_name+'<td class="right">'+items.add_time+'<td class="right">'+items.brand+'<td class="right">'+items.price+'';<br /> /*<td class="right"><a href="/Goods/edit/id/{$vo.goods_id}">编辑<td class="right"><a href="#">删除*/<br /> <br /> });<br /> html+=""; <br /> $(".goods-list").html(' ').html(html);<br /> // alert(html);<br /> }<br /> else if (data.status == 0) {<br /> $(".show_message").show();<br /> $(".show_message").html(data.info);<br /> $(".show_message").fadeOut(3000);<br /> // alert(data.info);<br /> <br /> return false;<br /> }<br /> }<br /> });<br /> }<br /> });<br /> });<br /> </script>这里是页面代码        <br> <div> <br>    <div> <br>      <h2>搜点什么呢</h2> <br>      <input><br>    </div> <br>    <a>搜 索</a><br> </div>

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn