自己写的一个JQuery自动完成例子
2010-08-20 10:51
621 查看
1,html部分
2,json数据源searchJson.json
3,css文件autocomplete.css
4效果图
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>search and autocomplete</title> <link href="../css/autocomplete.css" mce_href="css/autocomplete.css" rel="stylesheet" type="text/css" /> <mce:script src="../js/jquery-1.3.1.js" mce_src="js/jquery-1.3.1.js" type="text/javascript"><!-- // --></mce:script> <mce:script src="../js/jquery.autocomplete.js" mce_src="js/jquery.autocomplete.js" type="text/javascript"><!-- // --></mce:script> <mce:script type="text/javascript"><!-- $(function(){ $(".search>#searchText").keyup(function(e){ var $text = $(this); if ($text.val() != null && $text.val().trim() != "") { $.getJSON('searchJson.json', function(data){ var flag = 0; var indexOfPos = 0; $(".autoBox").empty();//先清楚弹出提示框的内容 var display = "<table><tr><td width=" + $text.css("width") + ">"; $.each(data, function(commentIndex, comment){//请求ajax数据(此处为json) if (!comment['searchText'].indexOf($text.val())) {//如果json里面有所输入的相应检索,而加入到html标签内 display += comment['searchText'] + "</td></tr><tr><td>"; flag = 1; } }) if (flag == 1) { display = display.substring(0, display.length - 8);//此地方减去后面8个字符的原因是因为上面display的后面会多一个"<tr><td>",故先去掉此字符串,再加上"</table>" display += "</table>"; $(".autoBox").html(display);//把数据表添加到弹出提示框 $(".shadow").show().slideDown(10000);//显示弹出提示框 $(".autoBox").css({ //弹出提示框的宽度设置 width: $text.css("width") }); $(".shadow").css({//弹出提示框的位置设置 top: $text.css("bottom"), left: ($text.offset().left + 3) + "px" }); $(document).keypress(function(e){//方向键事件 这个地方有点冲突 需要改进 先只贴上此部分代码 // alert("键盘事件="+e.keyCode) // alert("索引="+indexOfPos) // alert("长度="+$(".autoBox table tr").length) if(e.keyCode == 38){ //向上键 // alert("键盘事件:上") --indexOfPos; if(indexOfPos<1){ indexOfPos = 1; }else{ var currentTr = $(".autoBox table tr:nth-child("+indexOfPos+")"); $(".search>#searchText").val(currentTr.text()); currentTr.css({"background-color" : "#F000FF" , cursor : "pointer" }).siblings().css("background-color", "#E6EA00"); } }else if(e.keyCode == 40){ //向下键 // alert("键盘事件:下") ++indexOfPos; if(indexOfPos>$(".autoBox table tr").length){ indexOfPos = $(".autoBox table tr").length; }else{ var currentTr = $(".autoBox table tr:nth-child("+indexOfPos+")"); $(".search>#searchText").val(currentTr.text()); currentTr.css({"background-color" : "#F000FF" , cursor : "pointer" }).siblings().css("background-color", "#E6EA00"); } } }); $("table tr").css({//使弹出提示框的table中的tr左对齐 "text-align" : "left" }); $("table tr").hover(function(){//鼠标放在弹出提示框的table上时,执行的hover事件 $(this).css({"background-color" : "#F000FF" , cursor : "pointer" }); $(this).click(function(){ $(".search>#searchText").val($(this).text()); $(".shadow").hide(); }) }, function(){ $(this).css("background-color", "#E6EA00"); }); }else {//当没有相应的提示时,隐藏弹出提示框 $(".shadow").hide(); }; }) } else {//当输入框里面没有数据时 隐藏弹出提示框 $(".shadow").hide(); }; $(window).resize(function(){//窗口重置时能够保证弹出提示框框的位置永远处于输入框的下沿 $(".shadow").css({ top: $text.css("bottom"), left: ($text.offset().left + 3) + "px" }); }); }) }); // --></mce:script> </head> <body> <div class="search"> 搜索:<input id="searchText"/> </div> <div class="shadow"> <div class="autoBox"> </div> </div> </body> </html>
2,json数据源searchJson.json
[ { "searchText": "张三0" }, { "searchText": "张三00" }, { "searchText": "张三000" }, { "searchText": "李四0" }, { "searchText": "李四00" }, { "searchText": "李四000" }, { "searchText": "王五00" }, { "searchText": "张三11" }, { "searchText": "李四11" }, { "searchText": "王五11" }, { "searchText": "张三22" }, { "searchText": "李四22" }, { "searchText": "王五22" }, { "searchText": "张三33" }, { "searchText": "王五44" }, { "searchText": "张三55" }, { "searchText": "李四55" }, { "searchText": "王五55" } ]
3,css文件autocomplete.css
body{ text-align:center; margin:200px auto; } .autoBox{ float:left; border: 1px solid #A9C9E2; background-color: #E6EA00; margin-top: -3px; margin-bottom: 3px; margin-left: -3px; margin-right: 3px; } .shadow{ float:left; background :no-repeat right bottom #A9C9E2; position: absolute; border: 1px solid #A9C9E2; }
4效果图
相关文章推荐
- 30+最佳Ajax jQuery的自动完成插件的例子
- jQuery自己写的一个类似google自动提示的插件,源代码奉上
- 自己写的jQuery自动完成的插件(AutoComplete)
- 利用HTML5与jQuery技术创建一个简单的自动表单完成
- Jquery autocomplete-ajax自动完成例子
- 关于jquery ajax自动完成的一个小例子
- JS实现的一个简单的Autocomplete自动完成例子
- jquery的autocomplete 自动完成使用自己的css
- 利用HTML5与jQuery技术创建一个简单的自动表单完成
- 一个自动填充( autocomple)的例子(使用JQuery Cool Auto-Suggest)
- 近期用Jquery插件写的一个自动完成页面
- 发布自己写的jQuery自动完成的插件(AutoComplete)(28日更新)
- 锋利的jQuery-4--图片切换的一个例子(自己理解后写的,以备忘记时看看)
- 自己用户jquery加 web服务做的一个小例子
- 自己做的jquery的autocomplete的一个例子
- 自己做的jquery的autocomplete的一个例子
- JS实现的一个简单的Autocomplete自动完成例子
- jquery的autocompelete()自动完成事件以及后追加的页面元素无法绑定事件
- JavaScript学习笔记8--一个文字自动匹配的例子
- jQuery Autocomplete实现文本框自动完成搜索功能