首页 > PHP资讯 > HTML5培训技术 > 使用jQueryeach实现简单的搜索提示

使用jQueryeach实现简单的搜索提示

HTML5培训技术





jquery each实现简易的搜索提示功能
<script type="text/javascript"src="http://blog.163.com/qiuxinke2006%40126/blog/static/24885580201187091410/jquery.js">《script》
<script type="text/javascript">
//数据源,json的格式.
var user=[{"id": 1, "name": "张三","age":"25"},
{"id": 2, "name": "李思思","age":"35"},
{"id": 3, "name": "王五六","age":"20"},
{"id": 4, "name": "老赵","age":"20"},
{"id": 5, "name": "老张","age":"25"},
{"id": 6, "name": "李四","age":"35"},
{"id": 7, "name": "大刀王五","age":"20"},
{"id": 8, "name": "老王","age":"20"},
{"id": 9, "name": "abc","age":"25"},
{"id": 10, "name": "李b四","age":"35"},
{"id": 11, "name": "125","age":"20"},
{"id": 12, "name": "246","age":"20"},
{"id": 13, "name": "张三","age":"25"},
{"id": 14, "name": "李四","age":"35"},
{"id": 15, "name": "王老五","age":"20"},
{"id": 16, "name": "老钱","age":"20"},
{"id": 17, "name": "张三","age":"25"},
{"id": 18, "name": "李四","age":"35"},
{"id": 19, "name": "王五","age":"20"},
{"id": 20, "name": "老孙","age":"20"}];
$(document).ready(function(){
 var search = $("#search");
 search.keyup(function(event){
 //获取当前文本框的值
  var searchtext=$("#search").val();
  if(searchtext!=""){
   //构造显示页面
   var content = "";
   //遍历解析json
   $.each(user,function(id, item){
    if(item.name.indexOf(searchtext)!=-1){
     content +='

'+item.id+'
'+item.name+'
'+item.age+'
';
    }
   })
   $("#div").html(content);
  }else{
   $("#div").html("");
  }
 })
});

function transferValue(id){
 var name = $("#name_"+id).text();
 if(name){
  $("#search").val(name);
  $("#div").html("");
 }else{
  $("#search").val('');
 }
}

function changeColor(obj, item){
 if(item && item == 1){
  obj.className = "itemdiv itemdiv_color2";
 }else{
  obj.className = "itemdiv itemdiv_color1";
 }
}
《script》



名字:



HTML5培训技术

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持18不支持0