JSuggest自动匹配下拉框使用方法(示例代码)


Posted in Javascript onDecember 27, 2013

1.下载jquery-latest.js,JSuggest.js和JSuggest.css

JSuggest.js源代码如下

/**
* 
* Description : JSuggest 下拉提示框
*/
function JSuggest(){
// DIV下拉框
this.div = null;
// DIV下的ul
this.ul = null;
// 文本输入框
this.input = null;
// 当前DIV所选的LI对象
this.current_li = null;
/**
* 隐藏下拉提示框
*/
this.hide = function(){
this.div.style.visibility = "hidden";
}
/**
* 显示下拉提示框
*/
this.show = function(){
this.div.style.visibility = "visible";
}
/**
* 下拉提示框状态
*/
this.status = function(){
if (this.div.style.visibility == "visible"){
return true;
}
return false;
}
/**
* 设置当前DIV所选的LI
*/
this.setCurrent_li = function(li, obj){
var co = obj.current_li;
if (co != null){
co.className = ""; 
}
li.className = "li_index";
obj.current_li = li;
}
/**
* 初始化Suggest
* 
* input_id : 输入框的ID
* defHeight: 下拉提示框的高(不提供也可以)
*/
this.init = function(input_id, defHeight){
this.input = document.getElementById(input_id);
//this.input.autocomplete = "off";
var left = this.input.offsetLeft; 
var top = this.input.offsetTop;
var width = this.input.offsetWidth;
var height = this.input.offsetHeight;
var p=this.input.offsetParent;

 while(p!= null){
left+=p.offsetLeft;
top+=p.offsetTop;
p=p.offsetParent;

 }
top+= height;
if(defHeight==null || defHeight==0){
height = 150;
}else{
height = defHeight;
}
this.input.value = "";
var obj = this;
this.input.onkeydown = function(evt){
obj.onkeydown(evt, obj);
}
this.div = document.createElement("div");
this.div.style.width = width + "px";
this.div.style.height = height + "px";
this.div.style.left = left + "px";
this.div.style.top = top + "px";
this.ul = document.createElement("ul");
this.div.appendChild(this.ul);
this.div.className = "jsuggest";
document.body.appendChild(this.div);
}
/**
* 移除DIV下UL中所有的LI
*/
this.remove = function(){
this.current_li = null;
while(this.removeLI());
}
/**
* 移除DIV下UL中的LI
*/
this.removeLI = function(){
var node = this.ul.childNodes;
for(var n in node){
if (node[n] != null && node[n].nodeName == "LI"){
// alert(node[n].innerHTML);
this.ul.removeChild(node[n]);
return true;
}
}
return false;
}
/**
* 在DIV中创建LI
*/
this.create = function(items){
this.remove();
var li_item = items.split(",");
for(var i in li_item){
//alert(li_item[i]);
var li = document.createElement("li");
li.innerHTML = li_item[i];
var obj = this;
li.onmousedown = function(){
obj.onmousedown(this, obj);
}
li.onmouseover = this.onmouseover;
li.onmouseout = this.onmouseout;
this.ul.appendChild(li);
}
this.show();
}
/**
* 文本框按下事件
*/
this.onkeydown = function(evt, obj){
if (!obj.status()){
return false;
}
if (!evt && window.event)
{
evt = window.event;
}
var key = evt.keyCode;
//var KEYUP = 38;
//var KEYDOWN = 40;
//var KEYENTER = 13;
var ob = obj;
if (key == 38){
obj.upKeySelected();
}else if (key == 40){
obj.downKeySelected(); 
}else if (key == 13 || key == 27){
obj.hide(); 
}
}
this.getCurrentLiIndex = function(){
if(this.current_li == null){
return -1;
}
var node = this.ul.childNodes;
for(var n in node){
if (node[n].nodeName == "LI"){
if(node[n] == this.current_li){
return n;
}
}
} 
}
this.getLi = function(index){
var node = this.ul.childNodes;
for(var n in node){
if (node[n].nodeName == "LI" && n == index){
this.setCurrent_li(node[n], this);
return node[n];
}
}
}
this.upKeySelected = function(){
var num = this.getCurrentLiIndex();
if (num != -1 && num > 0){
num--;
var node = this.getLi(num);
this.setCurrent_li(node, this);
this.input.value = node.innerHTML;
}
}
this.downKeySelected = function(obj){
var num = this.getCurrentLiIndex();
if (num == -1){
num = 0; 
}else {
num++;
if (num >= this.ul.childNodes.length)return false;
}
var node = this.getLi(num);
this.setCurrent_li(node, this);
this.input.value = node.innerHTML;
}
/**
* DIV鼠标按下事件
*/
this.onmousedown = function(thiz, obj){
obj.setCurrent_li(thiz, obj);
obj.input.value = thiz.innerHTML;
obj.hide();
}
/**
* DIV鼠标移动事件
*/
this.onmouseover = function(){
if (this.className != "li_index"){
this.className = "li_check";
}
}
/**
* DIV鼠标移出事件
*/
this.onmouseout = function(){
if (this.className == "li_check"){
this.className = "";
}
}
}
var jsuggest = new JSuggest();

2.jsp页面
<input id="text" name="text" type="text"  onkeyup="go(event, this.value);"/>
<script type="text/javascript">    
 j(document).ready(function(){
  // 初始化JSUGGEST
  jsuggest.init("text");
  //或者用下面的方法,设置下拉框高度
  //jsuggest.init("text",200);
 })
 
 function go(event, value){
     event= event ? event : (window.event ? window.event : arguments[0]);
     var url = "url?suggestInput="+encodeURIComponent(value);//url是具体的action或jsp地址等,返回值是以逗号分隔的字符串
  goSuggestInput(event,url,value);
 }
 
function goSuggestInput(evnet,url,value){
        if (value == ""){
         // 如果输入框为空隐藏下拉框
         jsuggest.hide();
      return false;    
        }
     // 确保evt是一个有效的事件   
  if (!evnet && window.event)
  {
   evnet = window.event;
  }
  var key = evnet.keyCode;
  if (key == 38 || key == 40 || key == 13 || key == 27){
   return false;
  }
  j.ajax({
   type: "post",
         url:url,
   dataType: "text",
   cache: "false",
   beforeSend: function(XMLHttpRequest){
   },
   success: function(data, textStatus){
    // 对下拉框添加数据
    jsuggest.create(data);
   },
   complete: function(XMLHttpRequest, textStatus){
   },
   error: function(){
    alert("对不起,服务器忙!");
   }
  });
 }
</script>
Javascript 相关文章推荐
js 判断checkbox是否选中的实现代码
Nov 23 Javascript
jQuery新闻滚动插件 jquery.roller.js
Jun 27 Javascript
js汉字排序问题 支持中英文混排,兼容各浏览器,包括CHROME
Dec 20 Javascript
jquery1.9 下检测浏览器类型和版本的方法
Dec 26 Javascript
JavaScript的面向对象编程基础
Aug 13 Javascript
动态加载js、css的简单实现代码
May 26 Javascript
从零学习node.js之简易的网络爬虫(四)
Feb 22 Javascript
angularjs实现上拉加载和下拉刷新数据功能
Jun 12 Javascript
JS实现商品筛选功能
Aug 19 Javascript
jquery实现用户登陆界面(示例讲解)
Sep 06 jQuery
jQuery仿移动端支付宝键盘的实现代码
Aug 15 jQuery
Vue核心概念Action的总结
Jan 18 Javascript
jquery表单验证框架提供的身份证验证方法(示例代码)
Dec 27 #Javascript
JS 数字转换研究总结
Dec 26 #Javascript
利用jQuary实现文字浮动提示效果示例代码
Dec 26 #Javascript
js几秒以后倒计时跳转示例
Dec 26 #Javascript
Get中文乱码IE浏览器Get中文乱码解决方案
Dec 26 #Javascript
Jquery模仿Baidu、Google搜索时自动补充搜索结果提示
Dec 26 #Javascript
jquery1.9 下检测浏览器类型和版本的方法
Dec 26 #Javascript
You might like
php zlib压缩和解压缩swf文件的代码
2008/12/30 PHP
php生成静态文件的多种方法分享
2012/07/17 PHP
js将iframe中控件的值传到主页面控件中的实现方法
2013/03/11 Javascript
深入浅析javascript立即执行函数
2015/10/23 Javascript
深入浅析同源策略和跨域访问
2015/11/26 Javascript
js实现带三角符的手风琴效果
2017/03/01 Javascript
ES6新特性三: Generator(生成器)函数详解
2017/04/21 Javascript
bootstrap timepicker在angular中取值并转化为时间戳
2017/06/13 Javascript
Vue组件选项props实例详解
2017/08/18 Javascript
JavaScript键盘事件常见用法实例分析
2019/01/03 Javascript
微信小程序上传图片到php服务器的方法
2019/05/23 Javascript
Jquery 获取相同NAME 或者id删除行操作
2020/08/24 jQuery
Openlayers显示地理位置坐标的方法
2020/09/28 Javascript
jquery自定义组件实例详解
2020/12/31 jQuery
书单|人生苦短,你还不用python!
2017/12/29 Python
浅谈python str.format与制表符\t关于中文对齐的细节问题
2019/01/14 Python
Python之lambda匿名函数及map和filter的用法
2019/03/05 Python
Python列表list常用内建函数实例小结
2019/10/22 Python
python selenium 执行完毕关闭chromedriver进程示例
2019/11/15 Python
信号生成及DFT的python实现方式
2020/02/25 Python
基于virtualenv创建python虚拟环境过程图解
2020/03/30 Python
python 在threading中如何处理主进程和子线程的关系
2020/04/25 Python
Python实现封装打包自己写的代码,被python import
2020/07/12 Python
Python如何定义接口和抽象类
2020/07/28 Python
Python数据库封装实现代码示例解析
2020/09/05 Python
trivago美国:全球最大的酒店价格比较网站
2018/01/18 全球购物
西班牙太阳镜品牌:Hawkers
2018/03/11 全球购物
德国高品质男装及配饰商城:Cultizm(Raw Denim原色牛仔裤)
2018/04/16 全球购物
人力资源专业推荐信
2013/11/29 职场文书
个人授权委托书模板
2014/09/14 职场文书
上课迟到检讨书300字
2014/10/15 职场文书
生产车间管理制度
2015/08/04 职场文书
考教师资格证不要错过的4个最佳时机
2019/07/17 职场文书
导游词之上海杜莎夫人蜡像馆
2019/11/22 职场文书
python基础之停用词过滤详解
2021/04/21 Python
六种css3实现的边框过渡效果
2021/04/22 HTML / CSS