JSuggest automatically matches the drop down box using the method of of sample code

  • 2020-03-30 01:06:58
  • OfStack

1. Download jquery-latest.js, JSuggest. Js and JSuggest. CSS

The JSuggest. Js source code is as follows



function JSuggest(){
//DIV drop-down box
this.div = null;
//The ul DIV
this.ul = null;
//Text input field
this.input = null;
//The LI object selected by the current DIV
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;
}

this.setCurrent_li = function(li, obj){
var co = obj.current_li;
if (co != null){
co.className = ""; 
}
li.className = "li_index";
obj.current_li = li;
}

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);
}

this.remove = function(){
this.current_li = null;
while(this.removeLI());
}

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;
}

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;
}

this.onmousedown = function(thiz, obj){
obj.setCurrent_li(thiz, obj);
obj.input.value = thiz.innerHTML;
obj.hide();
}

this.onmouseover = function(){
if (this.className != "li_index"){
this.className = "li_check";
}
}

this.onmouseout = function(){
if (this.className == "li_check"){
this.className = "";
}
}
}
var jsuggest = new JSuggest();

2. The JSP page

<input id="text" name="text" type="text"  onkeyup="go(event, this.value);"/>
<script type="text/javascript">    

 j(document).ready(function(){
  //Initialize JSUGGEST
  jsuggest.init("text");
  //Or use the following method to set the height of the drop-down box
  //jsuggest.init("text",200);
 })

 
 function go(event, value){
     event= event ? event : (window.event ? window.event : arguments[0]);
     var url = "url?suggestInput="+encodeURIComponent(value);//The url is a concrete action or JSP address, etc., and the return value is a comma-separated string
  goSuggestInput(event,url,value);
 }
 
function goSuggestInput(evnet,url,value){
        if (value == ""){
         //Hide the drop-down box if the input box is empty
         jsuggest.hide();
      return false;    
        }

     //Ensure that evt is a valid event & NBSP;  
  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){
    //Add data to the drop-down box
    jsuggest.create(data);

   },
   complete: function(XMLHttpRequest, textStatus){

   },
   error: function(){
    alert(" I'm sorry , The server is busy !");
   }
  });
 }
</script>


Related articles: