// JScript 文件
var xmlHttp;
var currentLine = - 1;
var divtable;
function $(o)
{
return document.getElementById(o);
}
String.prototype.trim = function()
{
return this.replace(/(^[/s]*)|([/s]*$)/g, "");
}
function createXMLHttpRequest()
{
//开始初始化XMLHttpRequest 对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {//设置MiME 类别
xmlHttp.overrideMimeType("text/xml");
}
}else if (window.ActiveXObject) { // IE 浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlHttp) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest 对象实例.");
return false;
}
}
function getSearchResult(queryurl, values,event)
{
if ( values.trim() != "")
{
var keyCodes = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;//兼容FF IE
if(keyCodes != 38 && keyCodes != 40 && keyCodes != 32)
{
createXMLHttpRequest();
url = queryurl + values.trim();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
if(e.keyCode == 32)
{
dropDiv();
}
}
else
{
dropDiv();
}
}
function dropDiv()
{
if($("contents"))
{
document.body.removeChild(divtable);
}
}
function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
if(xmlHttp.responseText.trim() == "")
{
dropDiv();
}
else
{
createTable(xmlHttp.responseText);
}
}
}
}
function createTable(strtable)
{
if($("contents"))
{
document.body.removeChild(divtable);
}
divtable = document.createElement("div");
divtable.setAttribute("id", "contents");
divtable.style.position = "absolute";
divtable.style.border = "1px solid #817F82";
divtable.style.background = "#fff";
divtable.style.fontSize = "12px";
divtable.innerHTML = strtable;
divtable.zIndex = "400";
divtable.style.width = $("txtkecode").offsetWidth + 80 + "px";
divtable.style.overflowY = "auto";
divtable.style.overflowX = "hidden";
divtable.style.height = "200px";
divtable.style.top = $("txtkecode").offsetTop + $("txtkecode").offsetHeight + "px";
divtable.style.left = $("txtkecode").offsetLeft - 4 + "px";
document.body.appendChild(divtable);
}
document.onkeydown = function(e)
{
if(document.getElementById("ice"))
{
e = window.event || e;
switch(e.keyCode)
{
case 38 :
currentLine -- ;
changeItem();
break;
case 40 :
currentLine ++ ;
changeItem();
break;
case 32 :
document.body.appendChild(divtable);
break;
default :
break;
}
}
}
document.onclick = function()
{
dropDiv();
}
function changeItem()
{
if(document.all)
{
var it = document.getElementById("ice").children[0];
}
else
{
var it = document.getElementById("ice");
}
for(var j = 0; j < it.rows.length; j ++ )
{
it.rows[j].className = "";
}
if(currentLine < 0)
{
currentLine = it.rows.length - 1;
}
if(currentLine == it.rows.length)
{
currentLine = 0;
}
it.rows[currentLine].className = "highlight";
$("txtkecode").value = it.rows[currentLine].cells[0].innerHTML.split('-')[0];
$("txtkename").value = it.rows[currentLine].cells[0].innerHTML.split('-')[1];
}
function clickItem(clicktLine)
{
if(document.all)
{
var it = document.getElementById("ice").children[0];
}
else
{
var it = document.getElementById("ice");
}
for(i = 0; i < it.rows.length; i ++ )
{
it.rows[i].className = "";
}
it.rows[clicktLine].className = "highlight";
$("txtkecode").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[0];
$("txtkename").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[1];
it.rows[clicktLine].cells[0].onclick = function()
{
$("txtkecode").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[0];
$("txtkename").value = it.rows[clicktLine].cells[0].innerHTML.split('-')[1];
document.body.removeChild(divtable);
}
}
页面使用
<input type="text" id="txtkecode" name="txtkecode" οnkeyup="javascript:getSearchResult('do/Lession.aspx?daima=',this.value,event);"
style="position: relative;" />