JQuery can be used to implement the input search text drop down box

  • 2020-03-26 21:40:11
  • OfStack

Look at the effect
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201310/201310231802404.gif? 201392318257 ">  
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201310/201310231803235.gif? 201392318338 ">  
Function: Click the drop-down box input box, automatically display the following drop-down list, and is according to the input box input search add, automatically find in the background, and then to the foreground, display page.
Js:
 
var $xialaSELECT; 
$(document).ready(function(){ 
initXialaSelect(); 
initSearch(); 
}); 
var temptimeout=null; 
var query=""; 
function searchDev(key){ 
//if(key == "")return; 
query=key; 
clearTimeout(temptimeout); 
temptimeout= setTimeout(findUnSaved, 500); 
} 
function findUnSaved() 
{ 
//alert("dd"); 
//if(1==1)return; 
$.ajax({ 
type: "post", 
data:{'query':query}, 
url: path + "/tList.action", 
success: function(data) { 
xiala(data); 
}, 
error: function(data) { 
alert(" Failed to load, please try again! "); 
} 
}); 
} 
function initSearch() 
{ 
//Define a drop-down button layer and configure the style (position, anchor coordinates, size, background image, z-axis), appended to the text box
$xialaDIV = $('<div></div>').css('position', 'absolute').css('left', $('#province').position().left + $('#province').width() - 15 + 'px').css('top', 
$('#province').position().top + 4 + 'px').css('background', 'transparent url(../images/lala.gif) no-repeat top left').css('height', '16px').css('width', 
'15px').css('z-index', '100'); 
$('#province').after($xialaDIV); 
//Mouse into the modified background map position
$xialaDIV.mouseover(function(){ 
$xialaDIV.css('background-position', ' 0% -16px'); 
}); 
//Mouse out to modify the background position
$xialaDIV.mouseout(function(){ 
$xialaDIV.css('background-position', ' 0% -0px'); 
}); 
//Mouse down to modify the background position
$xialaDIV.mousedown(function(){ 
$xialaDIV.css('background-position', ' 0% -32px'); 
}); 
//Mouse release modifies the background position
$xialaDIV.mouseup(function(){ 
$xialaDIV.css('background-position', ' 0% -16px'); 
if($xialaSELECT) 
$xialaSELECT.show(); 
}); 
$('#province').mouseup(function(){ 
$xialaDIV.css('background-position', ' 0% -16px'); 
$xialaSELECT.show(); 
}); 
} 
var firstTimeYes=1; 
//The text box's drop-down div
function xiala(data){ 
//first time 
if($xialaSELECT) 
{ 
$xialaSELECT.empty(); 
} 
//Define a drop-down box layer and configure the style (position, anchor coordinates, width, z-axis), hiding it first
//Define five option layers, configure the style (width, Z axis must be higher than the height of the drop-down box), add the name, value attributes, and add the drop-down box layer
$xialaSELECT.append(data); 
if(firstTimeYes == 1) 
{ 
firstTimeYes =firstTimeYes+1; 
}else{ 
$xialaSELECT.show(); 
} 
} 
function initXialaSelect() 
{ 
$xialaSELECT = $('<div></div>').css('position', 'absolute').css('overflow-y','scroll').css('overflow-x','hidden').css('border', '1px solid #809DB9').css('border-top','none').css('left', '125px').css 
('top', $('#province').position().top + $('#province').height() + 6 + 'px').css('width', $('#province').width() + 'px').css('z-index', '101').css('width','152px').css('background','#fff').css('height','200px').css('max-height','600px'); 
$('#province').after($xialaSELECT); 
//Mouse - in - out style for the options layer
$xialaSELECT.mouseover(function(event){ 
if ($(event.target).attr('name') == 'option') { 
//When moving in, the background color becomes darker and the word color becomes white
$(event.target).css('background-color', '#000077').css('color', 'white'); 
$(event.target).mouseout(function(){ 
//Moving out makes the background color white and the word color black
$(event.target).css('background-color', '#FFFFFF').css('color', '#000000'); 
}); 
} 
}); 
//Click on the location to determine the pop-up display
$xialaSELECT.mouseup(function(event){ 
//If it is a drop-down button layer or a drop-down box layer, the drop-down box layer is still displayed
if (event.target == $xialaSELECT.get(0) || event.target == $xialaDIV.get(0)) { 
$xialaSELECT.show(); 
} 
else { 
//If it is the options layer, change the value of the text box
if ($(event.target).attr('name') == 'option') { 
//Pop up value observation
$('#nce').val($(event.target).html()); 
$('#d').val($(event.target).attr("d")); 
//if seleced host then hidden the dev type 
if($(event.target).attr("ass") == 3305) 
{ 
$("#ype").hide(); 
$("#ost").val(1); 
}else{ 
$("#ype").show(); 
$("#ost").val(-1); 
} 
} 
//If it is somewhere else, the box layer is pulled down
if ($xialaSELECT.css('display') == 'block') { 
$xialaSELECT.hide(); 
} 
} 
}); 
$xialaSELECT.hide(); 
} 
var k = 1; 
document.onclick = clicks; 
function clicks() 
{ 
if(k ==2){ 
k = 1; 
if($xialaSELECT) 
{ 
if ($xialaSELECT.css('display') == 'block') { 
$xialaSELECT.hide(); 
} 
} 
}else{ 
k = 2; 
} 
} 

 
.cba { 
width: 150px; 
height: 18px; 
} 
.selectlala { 
text-align: left; 
line-height: 20px; 
padding-left: 5px; 
width: 147px; 
font-family: Arial,verdana,tahoma; 
height: 20px; 
} 
<input type="text" name="province" id="province" class="cba" onkeyup="searchDev(this.value)"/> 

Related articles: